Skip to content

Commit 851d44a

Browse files
committed
Tidy: further refactoring:
- combine space and pipe cleaner - remove --format command line option - add test for missing END in for loop - add test for tidying pipe format to pipe format - fix pipe cleaner when input data has pipes
1 parent 9ee0f07 commit 851d44a

File tree

13 files changed

+187
-158
lines changed

13 files changed

+187
-158
lines changed

atest/robot/tidy/format_documentation.robot

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,4 @@ Suite Teardown Remove Directory ${TEMP} recursive=True
55

66
*** Test Cases ***
77
Documentation in text file
8-
Verify documentation formatting robot
9-
10-
*** Keywords ***
11-
Verify documentation formatting
12-
[Arguments] ${format}
13-
Run tidy and check result --format=${format} documentation.robot expected=documentation_expected.${format}
8+
Run tidy and check result ${EMPTY} documentation.robot expected=documentation_expected.robot

atest/robot/tidy/tidy.robot

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,22 @@ Tidying single test case file
88
[Documentation] Test tidying to different formats
99
[Template] Run tidy with golden file and check result
1010
${EMPTY} golden.robot
11-
--usepipes -f txt golden_pipes.robot
12-
--for robot golden.robot
11+
--usepipes golden_pipes.robot
12+
--usepipes golden_pipes.robot input=pipes-input.robot
1313
--spacecount 2 golden_two_spaces.robot
1414

1515
Tidying single resource file
1616
[Template] Run tidy with golden resource file and check result
1717
${EMPTY} golden_resource.robot
1818
-p golden_pipes_resource.robot
19-
--FOR ROBOT golden_resource.robot
2019

2120
Tidying single init file
2221
Run tidy and check result input=__init__.robot
2322
File Should Exist ${OUTFILE}
2423

2524
Tidying single file without output file prints output to console
2625
[Documentation] Depending on console encoding, non-ASCII characters may not be shown correctly.
27-
${stdout} = Run tidy input=golden.robot output=None
26+
${stdout} = Run tidy input=golden-input.robot output=None
2827
Compare tidy results ${stdout} golden.robot \\s+Log Many\\s+Non-ASCII:.*\\s+\\$\\{CURDIR\\}
2928
File Should Not Exist ${OUTFILE}
3029

@@ -35,7 +34,7 @@ Default format is got from output file
3534
Tidying directory
3635
[Setup] Copy Directory ${DATA}/tests ${TEMP}/tests
3736
${result_before}= Run Tests sources=${DATA}/tests
38-
Run Tidy --recursive --format robot ${TEMP}/tests
37+
Run Tidy --recursive ${TEMP}/tests
3938
Check file count ${TEMP}/tests *.robot 2
4039
Check file count ${TEMP}/tests/sub *.robot 1
4140
Check file count ${TEMP}/tests *.txt 0

atest/robot/tidy/tidy_resource.robot

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ ${OUTFILE} ${TEMP}${/}tidy-test-file.robot
99

1010
*** Keywords ***
1111
Run tidy with golden file and check result
12-
[Arguments] ${options} ${expected}
13-
${output} = Run tidy and check result ${options} golden-input.robot expected=${expected}
12+
[Arguments] ${options} ${expected} ${input}=golden-input.robot
13+
${output} = Run tidy and check result ${options} ${input} expected=${expected}
1414
[Return] ${output}
1515

1616
Run tidy with golden resource file and check result

atest/testdata/tidy/for_loops_expected.robot

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ For loop in test
44
Log ${x}
55
END
66

7+
Missing END
8+
FOR ${x} IN foo bar
9+
Log ${x}
10+
Keyword
11+
END
12+
713
Old for loop in test
814
FOR ${x} IN foo bar
915
Log ${x}

atest/testdata/tidy/for_loops_input.robot

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ For loop in test
44
Log ${x}
55
END
66

7+
Missing END
8+
FOR ${x} IN foo bar
9+
Log ${x}
10+
Keyword
11+
712
Old for loop in test
813
:FOR ${x} IN foo bar
914
\ Log ${x}

atest/testdata/tidy/pipes-input.robot

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
| This data is ignored at runtime but should be preserved by Tidy. |
2+
3+
| *** Settings *** |
4+
| Library | MyLibrary | argument | WITH NAME | My Alias | # My library comment |
5+
| Variables | MyVariables | args | args 2 | args 3 | args 4 | args 5 | args 6 |
6+
| ... | args 7 | args 8 | args 9 | args 10 | args 11 | args 12 |
7+
| Resource | resource.robot |
8+
|
9+
| *** Variables *** |
10+
| # standalone | comment |
11+
| ${VALID} | Value |
12+
| MyVar | val1 | val2 | val3 | val4 | val5 | val6 | val7 |
13+
| ... | val8 | val9 | val10 | # var comment |
14+
| # standalone |
15+
16+
| *** Test Cases *** |
17+
| My Test Case |
18+
| | [Documentation] | This is a documentation |
19+
| | ... | in two lines |
20+
| | My TC Step 1 | my step arg | # step 1 comment |
21+
| | My TC Step 2 | my step 2 arg | second \ arg | # step 2 comment |
22+
| | ... | third arg split to own row |
23+
| | ... | fourth and | fifth as well | # comment |
24+
| | [Teardown] | 1 minute | args |
25+
26+
| Another Test |
27+
| | Log Many | Non-ASCII: ääöö§§ | ${CURDIR} |
28+
29+
| *** Keyword *** |
30+
| My Keyword |
31+
| | [Documentation] | Documentation | # Comment for doc |
32+
| | [Tags] | keyword | tags |
33+
| | # Comment row |
34+
| | # Comment row 2 |
35+
| | My Step 1 | args | args 2 | args 3 | args 4 | args 5 | args 6 |
36+
| | ... | args 7 | args 8 | args 9 | # step 1 comment |
37+
| | FOR | ${param1} | ${param2} | IN | ${data 1} | ${data 2} | ${data 3} |
38+
| | ... | ${data 4} | ${data 5} | ${data 6} | # FOR comment |
39+
| | | Loop Step | args | args 2 | args 3 | args 4 | args 5 |
40+
| | | ... | args 6 | args 7 | args 8 | args 9 | # loop step comment |
41+
| | | Loop Step 2 |
42+
| | END |
43+
| | My Step 2 | my step 2 arg | second arg | # step 2 comment |
44+
| | [Return] | args 1 | args 2 |

src/robot/parsing/lexer/readers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def get_tokens(self):
125125
yield EOS.from_token(prev_token)
126126

127127
def _handle_old_for(self, statements):
128-
end_statement = [Token(Token.END)]
128+
end_statement = [Token(Token.SEPARATOR), Token(Token.END)]
129129
old_for = False
130130
for statement in statements:
131131
marker = self._get_first_data_token(statement)

src/robot/parsing/model/blocks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ def __init__(self, source=None):
4747
def has_tests(self):
4848
return any(isinstance(s, TestCaseSection) for s in self.sections)
4949

50-
def save(self, given_output, newline=None):
50+
def save(self, given_output):
5151
output = given_output if given_output else \
52-
file_writer(self.source, newline=newline)
52+
file_writer(self.source)
5353
FileWriter(output).visit(self)
5454
if given_output is None:
5555
output.close()

src/robot/parsing/model/statements.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,15 @@ def register(cls, subcls):
4848
cls._statement_handlers[subcls.type] = subcls
4949
if subcls.type == Token.KEYWORD:
5050
cls._statement_handlers[Token.ASSIGN] = subcls
51+
return cls
5152

5253
@classmethod
5354
def from_tokens(cls, tokens):
54-
if len(tokens) == 1 and tokens[0].type == Token.EOL:
55-
return EmptyLine(tokens)
5655
handlers = cls._statement_handlers
5756
for token in tokens:
5857
if token.type in handlers:
5958
return handlers[token.type](tokens)
60-
raise TypeError('Invalid statement: %r' % (tokens,))
59+
return EmptyLine(tokens)
6160

6261
@property
6362
def data_tokens(self):

src/robot/tidy.py

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,9 @@
3939

4040
from robot.errors import DataError
4141
from robot.parsing import get_model, SuiteStructureBuilder, SuiteStructureVisitor
42-
from robot.tidypkg import (Aligner, Cleaner, SeparatorCleaner, PipeAdder,
43-
NewlineAdder)
42+
from robot.tidypkg import Aligner, Cleaner, NewlineCleaner, SeparatorCleaner
4443
from robot.utils import Application, file_writer
4544

46-
# TODO: maybe rename --format to --extension
4745
# FIXME: Proofread usage
4846
USAGE = """robot.tidy -- Robot Framework test data clean-up tool
4947
@@ -71,9 +69,6 @@
7169
-r --recursive Process given directory recursively. Files in the directory
7270
are processed in-place similarly as when --inplace option
7371
is used. Does not process referenced resource files.
74-
-f --format txt|robot
75-
Output file format. If omitted, the format of the input
76-
file is used.
7772
-p --usepipes Use pipe ('|') as a cell separator in the plain text format.
7873
-s --spacecount number
7974
The number of spaces between cells in the plain text format.
@@ -139,11 +134,12 @@ class Tidy(SuiteStructureVisitor):
139134
Tidy command line options with same names.
140135
"""
141136

142-
def __init__(self, format='robot', use_pipes=False, space_count=4,
143-
line_separator=os.linesep):
144-
self._options = dict(format=format, pipe_separated=use_pipes,
145-
txt_separating_spaces=space_count,
146-
line_separator=line_separator)
137+
def __init__(self, space_count=4, use_pipes=False, line_separator=os.linesep):
138+
self.space_count = space_count
139+
self.use_pipes = use_pipes
140+
self.line_separator = line_separator
141+
self.short_test_name_length = 18
142+
self.setting_and_variable_name_length = 14
147143

148144
def file(self, path, outpath=None):
149145
"""Tidy a file.
@@ -160,8 +156,7 @@ def file(self, path, outpath=None):
160156
return writer.getvalue().replace('\r\n', '\n')
161157

162158
def _get_writer(self, outpath):
163-
return file_writer(outpath, newline=self._options['line_separator'],
164-
usage='Tidy output')
159+
return file_writer(outpath, usage='Tidy output')
165160

166161
def inplace(self, *paths):
167162
"""Tidy file(s) in-place.
@@ -183,13 +178,12 @@ def directory(self, path):
183178

184179
def _tidy(self, model, output=None):
185180
Cleaner().visit(model)
186-
NewlineAdder().visit(model)
187-
if self._options['pipe_separated']:
188-
PipeAdder().visit(model)
189-
else:
190-
SeparatorCleaner(self._options['txt_separating_spaces']).visit(model)
191-
Aligner().visit(model)
192-
model.save(output, self._options['line_separator'])
181+
NewlineCleaner(self.line_separator,
182+
self.short_test_name_length).visit(model)
183+
SeparatorCleaner(self.use_pipes, self.space_count).visit(model)
184+
Aligner(self.short_test_name_length,
185+
self.setting_and_variable_name_length).visit(model)
186+
model.save(output)
193187

194188
def visit_file(self, file):
195189
self.inplace(file.source)
@@ -211,9 +205,9 @@ class TidyCommandLine(Application):
211205
def __init__(self):
212206
Application.__init__(self, USAGE, arg_limits=(1,))
213207

214-
def main(self, arguments, recursive=False, inplace=False, format='txt',
208+
def main(self, arguments, recursive=False, inplace=False,
215209
usepipes=False, spacecount=4, lineseparator=os.linesep):
216-
tidy = Tidy(format=format, use_pipes=usepipes, space_count=spacecount,
210+
tidy = Tidy(use_pipes=usepipes, space_count=spacecount,
217211
line_separator=lineseparator)
218212
if recursive:
219213
tidy.directory(arguments[0])
@@ -227,7 +221,6 @@ def validate(self, opts, args):
227221
validator = ArgumentValidator()
228222
opts['recursive'], opts['inplace'] = validator.mode_and_args(args,
229223
**opts)
230-
opts['format'] = validator.format(args, **opts)
231224
opts['lineseparator'] = validator.line_sep(**opts)
232225
if not opts['spacecount']:
233226
opts.pop('spacecount')
@@ -267,16 +260,6 @@ def _default_mode_arguments(self, args):
267260
if not os.path.isfile(args[0]):
268261
raise DataError('Default mode requires input to be a file.')
269262

270-
def format(self, args, format, inplace, recursive, **others):
271-
if not format:
272-
if inplace or recursive or len(args) < 2:
273-
return None
274-
format = os.path.splitext(args[1])[1][1:]
275-
format = format.upper()
276-
if format not in ('TXT', 'ROBOT'):
277-
raise DataError("Invalid format '%s'." % format)
278-
return format
279-
280263
def line_sep(self, lineseparator, **others):
281264
values = {'native': os.linesep, 'windows': '\r\n', 'unix': '\n'}
282265
try:
@@ -303,7 +286,7 @@ def tidy_cli(arguments):
303286
304287
from robot.tidy import tidy_cli
305288
306-
tidy_cli(['--format', 'robot', 'tests.txt'])
289+
tidy_cli(['--spacecount', '2', 'tests.robot'])
307290
"""
308291
TidyCommandLine().execute_cli(arguments)
309292

0 commit comments

Comments
 (0)