Skip to content

Commit 2df5a34

Browse files
committed
better logging & fix ARMmbed#1
1 parent 8e58e2d commit 2df5a34

File tree

11 files changed

+157
-16
lines changed

11 files changed

+157
-16
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,3 +105,6 @@ ENV/
105105

106106
# editors
107107
*.idea
108+
109+
# test directory
110+
tmp_test_dir

src/snippet/config.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Config:
88
# IO
99
input_glob = 'tests/example/*.py'
10-
output_template = '```python\n# example: {{{name}}}\n{{{code}}}\n```' # a mustache template for each file
10+
output_template = '```python\n# example: {{{name}}}\n{{{code}}}\n```\n' # a mustache template for each file
1111
output_append = False # if the output file exists, append to it
1212
output_dir = None
1313
output_file_ext = 'md'

src/snippet/snippet.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,44 +25,46 @@ def extract_snippets(config: Config, lines, path):
2525
capture = True
2626
continue
2727

28+
current_debug_key = f'{current_key} ({line_num})'
29+
2830
if config.end_flag in line:
2931
# stop capturing, and discard empty blocks
3032
if not capture:
31-
raise exceptions.StartEndMismatch(f'Not yet capturing at {current_key}')
33+
raise exceptions.StartEndMismatch(f'Not yet capturing at {current_debug_key}')
3234
capture = False
3335
if not current_block:
3436
examples.pop(current_key)
3537
continue
3638

3739
if config.uncloak_flag in line:
3840
if not cloak:
39-
raise exceptions.CloakMismatch(f'Already uncloaked at {current_key}')
41+
raise exceptions.CloakMismatch(f'Already uncloaked at {current_debug_key}')
4042
cloak = False
4143
continue
4244

4345
if capture and not cloak:
4446
if config.cloak_flag in line:
4547
if cloak:
46-
raise exceptions.CloakMismatch(f'Already cloaked at {current_key}')
48+
raise exceptions.CloakMismatch(f'Already cloaked at {current_debug_key}')
4749
cloak = True
4850
continue
4951

5052
# whilst capturing, append code lines to the current block
51-
if config.fail_on_dedent and any(line[:current_strip].split(' ')):
52-
raise exceptions.ValidationFailure(f'Unexpected dedent whilst capturing {current_key}')
53+
if config.fail_on_dedent and any(line[:current_strip].lstrip()):
54+
raise exceptions.ValidationFailure(f'Unexpected dedent whilst capturing {current_debug_key}')
5355
clean_line = line[current_strip:].rstrip()
5456
for r_before, r_after in config.replacements.items():
5557
clean_line = clean_line.replace(r_before, r_after)
5658
for trigger in config.fail_on_contains:
5759
if trigger in clean_line:
58-
raise exceptions.ValidationFailure(f'Unexpected phrase {repr(trigger)} at {current_key}')
60+
raise exceptions.ValidationFailure(f'Unexpected phrase {repr(trigger)} at {current_debug_key}')
5961
# add this line of code to the example block
6062
current_block.append(clean_line)
6163

6264
if capture:
63-
raise exceptions.StartEndMismatch(f'EOF reached whilst still capturing {current_key}')
65+
raise exceptions.StartEndMismatch(f'EOF reached whilst still capturing {current_debug_key}')
6466

6567
if cloak:
66-
raise exceptions.CloakMismatch(f'EOF reached whilst still cloaked {current_key}')
68+
raise exceptions.CloakMismatch(f'EOF reached whilst still cloaked {current_debug_key}')
6769

6870
return examples

tests/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import os
2+
tmp_test_dirname = 'tmp_test_dir'
3+
tmp_test_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), tmp_test_dirname)

tests/samples/example.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import static org.junit.Assert.fail;
2+
3+
import java.util.Arrays;
4+
5+
import io.reactivex.BackpressureStrategy;
6+
7+
8+
public class Banana extends Fruit {
9+
10+
/**
11+
* Foos a Bar.
12+
* <p>
13+
* Note: This example introduces new high level abstraction objects
14+
*/
15+
16+
@Example
17+
public void fooABar() {
18+
Wibble wibbler = new Wobble(config);
19+
try {
20+
// an example: number 1
21+
// For more information about backpressure strategies, please have a look at related documentation:
22+
// https://github.com/ReactiveX/RxJava/wiki/Backpressure
23+
wibbler.subscribe(
24+
Thing1.newFilter().likeDevice("016%")
25+
.inDeviceStates(Arrays.asList(DeviceState.A, DeviceState.B)),
26+
BackpressureStrategy.BUFFER).flow().subscribe(System.out::println);
27+
28+
// Listening to device state changes for 2 minutes.
29+
Thread.sleep(120000);
30+
31+
// Stopping the Wobble.
32+
wibbler.stop();
33+
// end of example
34+
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
fail(e.getMessage());
38+
}
39+
}
40+
}

tests/samples/example.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# this is a python module
2+
3+
4+
class A:
5+
"""A class"""
6+
def my_example(self):
7+
# an example: number 1
8+
print('x: 5')
9+
for number in range(5):
10+
print(number)
11+
# end of example
12+
13+
14+
class B:
15+
# something unrelated
16+
pass

tests/samples/fixture.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
```python
2+
# example: number 1
3+
print('x: 5')
4+
for number in range(5):
5+
print(number)
6+
```
7+
```java
8+
# example: number 1
9+
// For more information about backpressure strategies, please have a look at related documentation:
10+
// https://github.com/ReactiveX/RxJava/wiki/Backpressure
11+
wibbler.subscribe(
12+
Thing1.newFilter().likeDevice("016%")
13+
.inDeviceStates(Arrays.asList(DeviceState.A, DeviceState.B)),
14+
BackpressureStrategy.BUFFER).flow().subscribe(System.out::println);
15+
16+
// Listening to device state changes for 2 minutes.
17+
Thread.sleep(120000);
18+
19+
// Stopping the Wobble.
20+
wibbler.stop();
21+
```

tests/test_config.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
import snippet
99

10+
from tests import tmp_test_dir
11+
1012

1113
class Test(unittest.TestCase):
12-
tmpdir = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'tmp_test_dir')
14+
tmpdir = tmp_test_dir
1315

1416
@classmethod
1517
def setUpClass(cls):

tests/test_direct.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import unittest
2+
import snippet
3+
import os
4+
import shutil
5+
import filecmp
6+
7+
from tests import tmp_test_dir
8+
9+
10+
class Test(unittest.TestCase):
11+
sample_input = os.path.join(os.path.dirname(__file__), 'samples')
12+
sample_output_dir = os.path.join(os.path.dirname(__file__), tmp_test_dir)
13+
14+
def setUp(self):
15+
if os.path.exists(self.sample_output_dir):
16+
shutil.rmtree(self.sample_output_dir)
17+
18+
def tearDown(self):
19+
shutil.rmtree(self.sample_output_dir)
20+
21+
def test_run(self):
22+
# writing two different languages sequentially to the same file
23+
24+
config = snippet.Config()
25+
config.output_dir = self.sample_output_dir
26+
config.output_append = True
27+
28+
# only detect the python file
29+
config.input_glob = 'tests/samples/example.py'
30+
snippet.main(config=config)
31+
32+
# only detect the java file
33+
config.output_template = '```java\n# example: {{{name}}}\n{{{code}}}\n```'
34+
config.input_glob = 'tests/samples/example.java'
35+
snippet.main(config=config)
36+
37+
self.assertTrue(filecmp.cmp(
38+
os.path.join(self.sample_output_dir, 'number1.md'),
39+
os.path.join(self.sample_input, 'fixture.md'),
40+
shallow=False,
41+
))

tests/test_parser.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,32 @@ def test_plain(self):
4444
self.go_exact(Config(), [start, 'test', newline, A, B, C, stop])
4545

4646
def test_indent(self):
47-
# left pad the sequence by two, to check result is dedented to depth of start
47+
# left pad the sequence by two spaces, to check result is dedented to depth of start
4848
sequence = [f' {x}' for x in [start + 'test' + newline, A, B, C]]
4949
sequence.append(stop)
5050
self.go_exact(
5151
Config(),
5252
sequence
5353
)
5454

55+
def test_indent_tabs(self):
56+
# left pad the sequence by two tabs, to check result is dedented to depth of start
57+
sequence = [f'\t\t{x}' for x in [start + 'test' + newline, A, B, C]]
58+
sequence.append(stop)
59+
self.go_exact(
60+
Config(),
61+
sequence
62+
)
63+
64+
def test_indent_with_blank_line(self):
65+
# left pad the sequence and then have some blank lines
66+
sequence = [f' {x}' for x in [start + 'test' + newline, A, B, C, stop]]
67+
sequence.insert(2, '')
68+
self.go_exact(
69+
Config(),
70+
sequence
71+
)
72+
5573
def test_trigger_phrase(self):
5674
with self.assertRaises(exceptions.ValidationFailure):
5775
self.go_exact(Config(), [start, 'test', newline, A, B, C, 'assert\n', stop])

0 commit comments

Comments
 (0)