Skip to content

Commit

Permalink
Support directory for -o argument
Browse files Browse the repository at this point in the history
This change adds support for -o/--output being a directory, with the
file name generated from the run id.
  • Loading branch information
briancurtin committed Mar 8, 2018
1 parent 57f1497 commit cdc8140
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
6 changes: 4 additions & 2 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@ for ``pine`` to run. See `Configuration`_ for details on this file.
By default, ``pine`` writes the test results in JSON format to standard
output, aka stdout, aka it prints them to the screen. If you'd like these
results written to a file, the ``-o`` (or ``--output``) argument takes a
file name to create. Note that it will overwrite any prior contents of that
file.
file name to create. You can also specify a directory for the output file
to be written to and ``pine`` will generate a file in that directory using
the run id it knows about from `-i`_. Note that in either case it will
overwrite any prior contents of that file.

``pine -c tests.yaml -o results.json`` will make pine run all of the tests
in tests.yaml and then write them to results.json.
Expand Down
17 changes: 13 additions & 4 deletions source/pine
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,15 @@ def get_args():
parser.add_argument("-c", "--config", dest="config_path",
help="Path to the configuraton file",
required=True)
parser.add_argument("-o", "--output", dest="output_path",
# Technically the default here is None,
# but we later interpret None to be sys.stdout
help="Path to the output file (default: sys.stdout)")
parser.add_argument(
"-o", "--output", dest="output_path",
# Technically the default here is None, but we later interpret
# None to be sys.stdout
help=("Path to write the output file (default: sys.stdout). "
"If this is a directory, the `-i` run id will be "
"the name of the file in that directory (with a '.json' "
"suffix). If this is a complete file path, the file "
"will be written there."))
parser.add_argument(
"-i", "--id", dest="run_id",
default=datetime.strftime(datetime.now(), "%Y%m%d%H%M%S%f"),
Expand Down Expand Up @@ -186,6 +191,10 @@ def write_output(results, output_path, run_id):
if output_path is None:
sys.stdout.write(output)
else:
# If we're given a directory, use the run_id to create the name.
if os.path.isdir(output_path):
output_path = os.path.join(output_path, run_id + ".json")

with open(output_path, "w") as out:
out.write(output)

Expand Down

0 comments on commit cdc8140

Please sign in to comment.