From fee300d4d7f380c846f0926de5a63e56921d0339 Mon Sep 17 00:00:00 2001 From: Tristan Lee Date: Tue, 19 Sep 2023 00:45:56 -0500 Subject: [PATCH] reorganized output directory parsing tests --- tests/cli.py | 25 +++++++++++++++++++------ tiktok_hashtag_analysis/cli.py | 2 +- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/tests/cli.py b/tests/cli.py index cf40f5a..6aa267e 100644 --- a/tests/cli.py +++ b/tests/cli.py @@ -41,44 +41,57 @@ def test_parser(hashtags, attribute, value, flag): assert args.get("hashtags") == hashtags -def test_process_output_dir(monkeypatch, tmp_path): - home_dir = Path.home().resolve() - +def test_output_dir_spec_noexist_nowrite(tmp_path): # Specified nonexistent output directory without write permissions parser = create_parser() - specified_output_dir = home_dir.parent / "test" + os.chmod(tmp_path, 0o444) + specified_output_dir = tmp_path / "test" with pytest.raises(SystemExit) as system_exit: result = process_output_dir( specified_output_dir=specified_output_dir, parser=parser ) assert system_exit.type == SystemExit + +def test_output_dir_spec_exist_nowrite(tmp_path): # Specified existing output directory without write permissions parser = create_parser() - specified_output_dir = home_dir.parent + os.chmod(tmp_path, 0o444) + specified_output_dir = tmp_path with pytest.raises(SystemExit) as system_exit: result = process_output_dir( specified_output_dir=specified_output_dir, parser=parser ) assert system_exit.type == SystemExit + +def test_output_dir_unspec_nowrite(monkeypatch, tmp_path): # Unspecified, in current directory without write permissions + parser = create_parser() cwd = os.getcwd() + specified_output_dir = tmp_path monkeypatch.chdir(specified_output_dir) + os.chmod(tmp_path, 0o444) result = process_output_dir(specified_output_dir=None, parser=parser) monkeypatch.chdir(cwd) assert result == DEFAULT_OUTPUT_DIR + +def test_output_dir_spec_noexist_write(tmp_path): # Specified nonexisting output directory with write permissions parser = create_parser() - specified_output_dir = tmp_path / "test" / "tiktok" + specified_output_dir = tmp_path / "test" result = process_output_dir( specified_output_dir=specified_output_dir, parser=parser ) assert result == specified_output_dir + +def test_output_dir_unspec_write(monkeypatch, tmp_path): # Unspecified, in current directory with write permissions + parser = create_parser() cwd = os.getcwd() + specified_output_dir = tmp_path monkeypatch.chdir(specified_output_dir) result = process_output_dir(specified_output_dir=None, parser=parser) monkeypatch.chdir(cwd) diff --git a/tiktok_hashtag_analysis/cli.py b/tiktok_hashtag_analysis/cli.py index b2583cd..778c28f 100644 --- a/tiktok_hashtag_analysis/cli.py +++ b/tiktok_hashtag_analysis/cli.py @@ -101,7 +101,7 @@ def process_output_dir( else: # On Windows, os.access is unreliable temp_file = _output_dir / "test.txt" - with open(temp_file, 'w') as f: + with open(temp_file, "w") as f: f.write("test") os.remove(temp_file) return _output_dir