Skip to content

Commit

Permalink
Changed the behavior of the app to output the original Postman CLI ou…
Browse files Browse the repository at this point in the history
…tput. Output location of the json via filename is now required. Also will now exit with exit_code 1 if underlying CLI output contains test run errors. This can be turned off via cmd line flag if unwanted.
  • Loading branch information
cerdmann committed Sep 16, 2024
1 parent d2f9439 commit 905bd13
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 10 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ To output both JSON and a Junit formatted xml file use:
postman collection run 11111111-11111111-1111-1111-1111-111111111111 -e 11111111-11111111-1111-1111-1111-111111111111 | postman-cli-transformer output.json --junit-out-file junit.xml
```

Furthermore, the tool will exit with a code of 1 if any of the tests run by the CLI fail. This behavior may be turned off by a flag.

## Development

To contribute to this tool, first checkout the code. Then create a new virtual environment:
Expand Down
37 changes: 27 additions & 10 deletions postman_cli_transformer/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,34 @@


@click.command()
@click.argument("output", type=click.File("w"), default="-", required=False)
@click.argument("output", type=click.File("w"), required=True)
@click.option(
"-junit",
"--junit-out-file",
required=False,
type=click.File("w"),
help="File location to output junit xml file from transformed CLI results.",
)
@click.option(
"-bup",
"--bubble-up-exit-code",
default=True,
required=False,
type=click.BOOL,
help="""Defaults to True. Since this tool is used to transform output results from the Postman CLI,
it will exit with an error if the underlying Postman CLI output contains an error.
This will facilitate the failure of the task in a CI/CD pipeline. If you do not want
this behavior and wish the exit code to reflect the exit state of this app, set
this flag to False.""",
)
@click.version_option()
def cli(output, junit_out_file):
def cli(output, junit_out_file, bubble_up_exit_code):
"""This script will take as input the STDOUT from
a Postman CLI collection run and transform the
output text to a file containing the output data
organized in a JSON format. This JSON data is then
written into a specific file.
\b
Output to stdout:
postman-cli-transformer
organized in a JSON format. It will also preserve
the CLI standard out and send to STDOUT at the end
of its execution.
\b
Output to file foo.json:
Expand All @@ -40,7 +50,7 @@ def cli(output, junit_out_file):

stdin_data = sys.stdin.read()

parsed_stdin = parse(stdin_data)
parsed_stdin, errored = parse(stdin_data)

if junit_out_file:
current_time_of_test_run = datetime.now().isoformat()
Expand All @@ -51,6 +61,12 @@ def cli(output, junit_out_file):
output.write(parsed_stdin)
output.flush()

click.echo(stdin_data)

if bubble_up_exit_code:
if errored:
raise click.exceptions.Exit(1)


def parse(data):
raw_lines = []
Expand All @@ -60,7 +76,8 @@ def parse(data):

processor = Processor(raw_lines)
results = processor.parsed
errored = processor.errored

json_str = json.dumps(results)

return json_str
return json_str, errored
2 changes: 2 additions & 0 deletions postman_cli_transformer/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ def __init__(self, lines_to_process):
# Class property definiing
self.processing_helper = ProcessingHelper()
self.parsed = {"collectionName": "", "folders": []}
self.errored = False
self.lines_to_process = lines_to_process

# Get header info out of the way
Expand Down Expand Up @@ -167,6 +168,7 @@ def _process_rest_of_lines(self):
self.processing_helper.update_current_line_type(
LINE_TYPES.ERROR_HEADER_LINE
)
self.errored = True

case LINE_TYPES.ERROR_LINE:
self.processing_helper.update_current_line_type(
Expand Down
File renamed without changes.
12 changes: 12 additions & 0 deletions tests/test_processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ def test_should_be_able_to_process_folders():

processor = Processor(lines)
results = processor.parsed
errored = processor.errored

expected_results = {
"collectionName": "Pinball Map Collection",
Expand All @@ -48,6 +49,7 @@ def test_should_be_able_to_process_folders():
}

assert json.dumps(results) == json.dumps(expected_results)
assert errored is False


def test_should_be_able_to_process_urls():
Expand All @@ -73,6 +75,7 @@ def test_should_be_able_to_process_urls():

processor = Processor(lines)
results = processor.parsed
errored = processor.errored

expected_results = {
"collectionName": "Pinball Map Collection",
Expand Down Expand Up @@ -145,6 +148,7 @@ def test_should_be_able_to_process_urls():
}

assert json.dumps(results) == json.dumps(expected_results)
assert errored is False


def test_should_be_able_to_process_tests():
Expand Down Expand Up @@ -182,6 +186,7 @@ def test_should_be_able_to_process_tests():

processor = Processor(lines)
results = processor.parsed
errored = processor.errored

expected_results = {
"collectionName": "Pinball Map Collection",
Expand Down Expand Up @@ -353,6 +358,7 @@ def test_should_be_able_to_process_tests():
}

assert json.dumps(results) == json.dumps(expected_results)
assert errored is False


def test_should_be_able_to_process_root_level_requests():
Expand Down Expand Up @@ -400,6 +406,7 @@ def test_should_be_able_to_process_root_level_requests():

processor = Processor(lines)
results = processor.parsed
errored = processor.errored

expected_results = {
"collectionName": "Pinball Map Collection",
Expand Down Expand Up @@ -624,6 +631,7 @@ def test_should_be_able_to_process_root_level_requests():
}

assert json.dumps(results) == json.dumps(expected_results)
assert errored is False


def test_should_be_able_to_process_the_summary_table():
Expand Down Expand Up @@ -667,6 +675,7 @@ def test_should_be_able_to_process_the_summary_table():

processor = Processor(lines)
results = processor.parsed
errored = processor.errored

expected_results = {
"collectionName": "Deactivate User Accounts",
Expand Down Expand Up @@ -745,6 +754,7 @@ def test_should_be_able_to_process_the_summary_table():
}

assert json.dumps(results) == json.dumps(expected_results)
assert errored is False


def test_should_be_able_to_process_error_descriptions():
Expand Down Expand Up @@ -810,6 +820,7 @@ def test_should_be_able_to_process_error_descriptions():

processor = Processor(lines)
results = processor.parsed
errored = processor.errored

expected_results = {
"collectionName": "Pinball Map Collection",
Expand Down Expand Up @@ -963,3 +974,4 @@ def test_should_be_able_to_process_error_descriptions():
}

assert json.dumps(results) == json.dumps(expected_results)
assert errored is True

0 comments on commit 905bd13

Please sign in to comment.