Skip to content

Commit

Permalink
sc-12505 add exclude-dirs option for directory walking command (#10)
Browse files Browse the repository at this point in the history
* sc-12505 add exclude-dirs option for directory walking command

---------

Co-authored-by: Matthew Warren <matt.warren+github-commits@cloudtruth.com>
  • Loading branch information
mattwwarren and mattwwarren authored Mar 12, 2024
1 parent 61aaf6f commit b73e4e9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/dynamic_importer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,19 @@ def create_data(data_file, template_file, project, k, c, u):
required=True,
multiple=True,
)
@click.option(
"--exclude-dirs",
help="Directory to exclude from walking. Can be specified multiple times",
multiple=True,
)
@click.option(
"-o",
"--output-dir",
help="Directory to write processed output to. Default is current directory",
default=".",
required=False,
)
def walk_directories(config_dir, file_types, output_dir):
def walk_directories(config_dir, file_types, exclude_dirs, output_dir):
walked_files = {}
output_dir = output_dir.rstrip("/")
for root, dirs, files in os.walk(config_dir):
Expand All @@ -251,6 +256,13 @@ def walk_directories(config_dir, file_types, output_dir):
if dir in dirs:
dirs.remove(dir)

# skip over user-specified non-config directories
for dir in exclude_dirs:
dir = dir.rstrip("/")
if os.path.abspath(dir) in [f"{os.path.abspath(root)}/{d}" for d in dirs]:
click.echo(f"Excluding directory: {os.path.abspath(dir)}")
dirs.remove(os.path.basename(dir))

walked_files.update(walk_files(root, files, file_types))

project_files = defaultdict(lambda: defaultdict(list))
Expand Down
58 changes: 56 additions & 2 deletions src/tests/test_directory_walking.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
"""
Hey-o! Warren here. walk-directories prompts the user for information
for every file in the supplied directory to walk. Therefore, the tests
MUST supply input for the prompts. Otherwise, your tests will just
hang indefinitely.
MUST supply input for the prompts. If you write a test that supplies
prompt input, please use the `@pytest.mark.timeout(30)` decorator to
avoid hanging indefinitely.
"""


Expand Down Expand Up @@ -121,3 +122,56 @@ def test_walk_directories_multiple_file_types(tmp_path):

assert not pathlib.Path(f"{td}/myproj-tf.ctconfig").exists()
assert not pathlib.Path(f"{td}/myproj-tf.cttemplate").exists()


@pytest.mark.timeout(30)
@pytest.mark.usefixtures("tmp_path")
def test_walk_directories_with_exclusion(tmp_path):
runner = CliRunner()
current_dir = pathlib.Path(__file__).parent.resolve()

prompt_responses = [
"", # processing dotenv file
"myproj",
"default",
"", # processing yaml file
"", # using myproj default
"default", # use default environment
"", # skipping json file
"", # skipping tfvars file
"", # skipping tf file
"",
"",
]
with runner.isolated_filesystem(temp_dir=tmp_path) as td:
result = runner.invoke(
import_config,
[
"walk-directories",
"-t",
"dotenv",
"-t",
"yaml",
"-c",
f"{current_dir}/../../samples",
"--exclude-dirs",
f"{current_dir}/../../samples/dotenvs",
"--output-dir",
td,
],
input="\n".join(prompt_responses),
catch_exceptions=False,
)
assert result.exit_code == 0

assert pathlib.Path(f"{td}/myproj-dotenv.ctconfig").exists()
assert pathlib.Path(f"{td}/myproj-dotenv.cttemplate").exists()
assert os.path.getsize(f"{td}/myproj-dotenv.ctconfig") > 0
assert os.path.getsize(f"{td}/myproj-dotenv.cttemplate") > 0

assert pathlib.Path(f"{td}/myproj-yaml.ctconfig").exists()
assert pathlib.Path(f"{td}/myproj-yaml.cttemplate").exists()
assert os.path.getsize(f"{td}/myproj-yaml.ctconfig") > 0
assert os.path.getsize(f"{td}/myproj-yaml.cttemplate") > 0

assert len(os.listdir(pathlib.Path(f"{td}/"))) == 4

0 comments on commit b73e4e9

Please sign in to comment.