Skip to content

Commit 9b4cae3

Browse files
committed
Tidy: Enhance handling invalid output.
Also add tests for it and cleanup others. Fixes robotframework#3339.
1 parent 1aad86a commit 9b4cae3

File tree

5 files changed

+53
-25
lines changed

5 files changed

+53
-25
lines changed

atest/robot/tidy/TidyLib.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
ROBOT_SRC = join(dirname(abspath(__file__)), '..', '..', '..', 'src')
1212
DATA_DIR = join(dirname(abspath(__file__)), '..', '..', 'testdata', 'tidy')
13-
TEMP_FILE = join(os.getenv('TEMPDIR'), 'tidy-test-dir', 'tidy-test-file.txt')
13+
OUTFILE = join(os.getenv('TEMPDIR'), 'tidy-test-dir', 'tidy-test-file.robot')
1414

1515

1616
class TidyLib(object):
@@ -19,35 +19,35 @@ def __init__(self, interpreter):
1919
self._tidy = interpreter.tidy
2020
self._interpreter = interpreter.interpreter
2121

22-
def run_tidy(self, options, input, output=None, tidy=None, stderr=True):
22+
def run_tidy(self, options=None, input=None, output=None, tidy=None, rc=0):
2323
"""Runs tidy in the operating system and returns output."""
2424
command = (tidy or self._tidy)[:]
2525
if options:
2626
command.extend(shlex.split(options))
27-
command.append(self._path(input))
27+
if input:
28+
command.append(self._path(input))
2829
if output:
2930
command.append(output)
3031
print(' '.join(command))
31-
# FIXME: Possibility to exclude stderr is a hack and shuold be removed
32-
# as part of implementing #3064.
3332
result = run(command,
3433
cwd=ROBOT_SRC,
3534
stdout=PIPE,
36-
stderr=STDOUT if stderr else PIPE,
35+
stderr=STDOUT,
3736
universal_newlines=True,
3837
shell=os.sep == '\\')
39-
if result.returncode != 0:
40-
raise RuntimeError(result.stdout)
38+
if result.returncode != rc:
39+
raise RuntimeError(f'Expected Tidy to return {rc} but it returned '
40+
f'{result.returncode}.')
4141
return result.stdout.rstrip()
4242

43-
def run_tidy_and_check_result(self, options, input, output=TEMP_FILE,
44-
expected=None):
43+
def run_tidy_and_check_result(self, options=None, input=None,
44+
output=OUTFILE, expected=None):
4545
"""Runs tidy and checks that output matches content of file `expected`."""
4646
result = self.run_tidy(options, input, output)
4747
return self.compare_tidy_results(output or result, expected or input)
4848

49-
def run_tidy_as_script_and_check_result(self, options, input,
50-
output=TEMP_FILE, expected=None):
49+
def run_tidy_as_script_and_check_result(self, options=None, input=None,
50+
output=OUTFILE, expected=None):
5151
"""Runs tidy and checks that output matches content of file `expected`."""
5252
tidy = self._interpreter + [join(ROBOT_SRC, 'robot', 'tidy.py')]
5353
result = self.run_tidy(options, input, output, tidy)

atest/robot/tidy/invalid_usage.robot

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
*** Settings ***
2+
Test Template Tidy run should fail
3+
Resource tidy_resource.robot
4+
5+
*** Test Cases ***
6+
Too few arguments
7+
Expected at least 1 argument, got 0.
8+
9+
Invalid option
10+
option --nonex not recognized --nonex
11+
12+
Non-existing input
13+
Default mode requires input to be a file. ${EMPTY} nonex.robot
14+
--inplace requires inputs to be files. --inplace nonex.robot
15+
--recursive requires input to be a directory. --recursive nonex
16+
17+
Invalid output
18+
[Setup] Create Directory ${OUTFILE}
19+
Opening Tidy output file '${OUTFILE}' failed: *Error: *
20+
... input=golden.robot output=${OUTFILE}
21+
[Teardown] Remove Directory ${OUTFILE}
22+
23+
*** Keywords ***
24+
Tidy run should fail
25+
[Arguments] ${error} @{args} &{kwargs}
26+
${output} = Run Tidy @{args} &{kwargs} rc=252
27+
Should Match ${output} ${error}${USAGE TIP}

atest/robot/tidy/tidy.robot

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,22 @@ Tidying single test case file
1414

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

2121
Tidying single init file
22-
Run tidy and check result ${EMPTY} __init__.robot
23-
File Should Exist ${TEMP FILE}
22+
Run tidy and check result input=__init__.robot
23+
File Should Exist ${OUTFILE}
2424

2525
Tidying single file without output file prints output to console
2626
[Documentation] Depending on console encoding, non-ASCII characters may not be shown correctly.
27-
${stdout} = Run tidy ${EMPTY} golden.robot output=None stderr=False
27+
${stdout} = Run tidy input=golden.robot output=None
2828
Compare tidy results ${stdout} golden.robot \\s+Log Many\\s+Non-ASCII:.*\\s+\\$\\{CURDIR\\}
29-
File Should Not Exist ${TEMP FILE}
29+
File Should Not Exist ${OUTFILE}
3030

3131
Default format is got from output file
32-
Run tidy ${EMPTY} ${DATA}/golden.robot ${TEMP}/golden.txt
32+
Run tidy input=${DATA}/golden.robot output=${TEMP}/golden.txt
3333
Compare tidy results ${TEMP}/golden.txt ${DATA}/golden.txt
3434

3535
Tidying directory
@@ -46,14 +46,14 @@ Tidying directory
4646
Should Be Equal ${result_before.stdout} ${result_after.stdout}
4747

4848
Custom headers are preserved and tables aligned accordingly
49-
Run tidy and check result ${EMPTY} golden_with_headers.robot
49+
Run tidy and check result input=golden_with_headers.robot
5050

5151
Running Tidy as script
5252
[Tags] no-standalone
53-
Run tidy as script and check result ${EMPTY} golden.robot
53+
Run tidy as script and check result input=golden.robot
5454

5555
For loops
56-
Run tidy and check result ${EMPTY} for_loops_input.robot
56+
Run tidy and check result input=for_loops_input.robot
5757
... expected=for_loops_expected.robot
5858

5959
*** Keywords ***

atest/robot/tidy/tidy_resource.robot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Resource atest_resource.robot
55
*** Variables ***
66
${DATA} ${CURDIR}/../../testdata/tidy
77
${TEMP} %{TEMPDIR}${/}tidy-test-dir
8-
${TEMPFILE} ${TEMP}${/}tidy-test-file.txt
8+
${OUTFILE} ${TEMP}${/}tidy-test-file.robot
99

1010
*** Keywords ***
1111
Run tidy with golden file and check result

src/robot/tidy.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ def file(self, path, outpath=None):
163163
return writer.getvalue().replace('\r\n', '\n')
164164

165165
def _get_writer(self, outpath):
166-
return file_writer(outpath, newline=self._options['line_separator'])
166+
return file_writer(outpath, newline=self._options['line_separator'],
167+
usage='Tidy output')
167168

168169
def inplace(self, *paths):
169170
"""Tidy file(s) in-place.

0 commit comments

Comments
 (0)