diff --git a/README.md b/README.md index 03c71eda..3fd7e35a 100644 --- a/README.md +++ b/README.md @@ -510,7 +510,7 @@ Usage: kiplot [-b BOARD] [-e SCHEMA] [-c CONFIG] [-d OUT_DIR] [-s PRE] [-q | -v...] [-i] [TARGET...] kiplot [-c PLOT_CONFIG] --list - kiplot [-b BOARD] --example + kiplot [-b BOARD] [-d OUT_DIR] --example kiplot --help-list-outputs kiplot --help-output=HELP_OUTPUT kiplot --help-outputs diff --git a/kiplot/__main__.py b/kiplot/__main__.py index 9965ab8f..37d4a679 100644 --- a/kiplot/__main__.py +++ b/kiplot/__main__.py @@ -5,7 +5,7 @@ Usage: kiplot [-b BOARD] [-e SCHEMA] [-c CONFIG] [-d OUT_DIR] [-s PRE] [-q | -v...] [-i] [TARGET...] kiplot [-c PLOT_CONFIG] --list - kiplot [-b BOARD] --example + kiplot [-b BOARD] [-d OUT_DIR] --example kiplot --help-list-outputs kiplot --help-output=HELP_OUTPUT kiplot --help-outputs @@ -172,7 +172,7 @@ def main(): sys.exit(0) if args.example: check_board_file(args.board_file) - create_example(args.board_file) + create_example(args.board_file, GS.out_dir) sys.exit(0) # Determine the YAML file diff --git a/kiplot/config_reader.py b/kiplot/config_reader.py index 0c77ba46..a792eebd 100644 --- a/kiplot/config_reader.py +++ b/kiplot/config_reader.py @@ -303,12 +303,15 @@ def print_preflights_help(): print('- {}: {}.'.format(n, help.rstrip())) -def create_example(pcb_file): - if os.path.isfile(EXAMPLE_CFG): - logger.error(EXAMPLE_CFG+" already exists, won't overwrite") +def create_example(pcb_file, out_dir): + if not os.path.exists(out_dir): + os.makedirs(out_dir) + fname = os.path.join(out_dir, EXAMPLE_CFG) + if os.path.isfile(fname): + logger.error(fname+" already exists, won't overwrite") exit(WONT_OVERWRITE) - with open(EXAMPLE_CFG, 'w') as f: - logger.info('Creating {} example configuration'.format(EXAMPLE_CFG)) + with open(fname, 'w') as f: + logger.info('Creating {} example configuration'.format(fname)) f.write('kiplot:\n version: 1\n') # Preflights f.write('\npreflight:\n') diff --git a/tests/test_plot/test_misc.py b/tests/test_plot/test_misc.py index 86904046..9d7d981d 100644 --- a/tests/test_plot/test_misc.py +++ b/tests/test_plot/test_misc.py @@ -18,6 +18,9 @@ Tests miscellaneous stuff. - Guess the SCH and YAML - Guess the SCH and YAML when more than one is present - --list +- Create example + - with PCB + - already exists For debug information use: pytest-3 --log-cli-level debug @@ -35,7 +38,7 @@ from utils import context prev_dir = os.path.dirname(prev_dir) if prev_dir not in sys.path: sys.path.insert(0, prev_dir) -from kiplot.misc import (EXIT_BAD_ARGS, EXIT_BAD_CONFIG, NO_PCB_FILE, NO_SCH_FILE) +from kiplot.misc import (EXIT_BAD_ARGS, EXIT_BAD_CONFIG, NO_PCB_FILE, NO_SCH_FILE, EXAMPLE_CFG, WONT_OVERWRITE) POS_DIR = 'positiondir' @@ -334,3 +337,29 @@ def test_help_preflights(): ctx.run(extra=['--help-preflights'], no_verbose=True, no_out_dir=True, no_yaml_file=True, no_board_file=True) assert ctx.search_out('Supported preflight options') ctx.clean_up() + + +def test_example_1(): + ctx = context.TestContext('Example1', '3Rs', 'pre_and_position', '') + ctx.run(extra=['--example'], no_verbose=True, no_yaml_file=True, no_board_file=True) + assert ctx.expect_out_file(EXAMPLE_CFG) + os.remove(ctx.get_out_path(EXAMPLE_CFG)) + ctx.clean_up() + + +def test_example_2(): + ctx = context.TestContext('Example2', 'good-project', 'pre_and_position', '') + ctx.run(extra=['--example'], no_verbose=True, no_yaml_file=True) + assert ctx.expect_out_file(EXAMPLE_CFG) + ctx.search_in_file(EXAMPLE_CFG, ['GND.Cu']) + os.remove(ctx.get_out_path(EXAMPLE_CFG)) + ctx.clean_up() + + +def test_example_3(): + ctx = context.TestContext('Example3', 'good-project', 'pre_and_position', '') + ctx.run(extra=['--example'], no_verbose=True, no_yaml_file=True) + assert ctx.expect_out_file(EXAMPLE_CFG) + ctx.run(WONT_OVERWRITE, extra=['--example'], no_verbose=True, no_yaml_file=True) + os.remove(ctx.get_out_path(EXAMPLE_CFG)) + ctx.clean_up()