diff --git a/tests/integration/conftest.py b/tests/integration/conftest.py index b2ad462ee..301d51fd6 100644 --- a/tests/integration/conftest.py +++ b/tests/integration/conftest.py @@ -33,6 +33,13 @@ async def session(): yield ps +@pytest.fixture +def cwd_tmp_path(tmp_path, monkeypatch): + """Run the test with cwd set to a fresh tmp_path, restored afterwards.""" + monkeypatch.chdir(tmp_path) + return tmp_path + + @pytest.fixture def order_descriptions(order_description): order1 = order_description diff --git a/tests/integration/test_data_cli.py b/tests/integration/test_data_cli.py index 0ebee6f8f..827bd7ab9 100644 --- a/tests/integration/test_data_cli.py +++ b/tests/integration/test_data_cli.py @@ -976,7 +976,8 @@ def test_asset_download_default(invoke, item_type, item_id, asset_type, - dl_url): + dl_url, + tmp_path): mock_asset_get_response() @@ -1002,36 +1003,36 @@ async def _stream_img(): respx.get(dl_url).return_value = mock_resp_download runner = CliRunner() - with runner.isolated_filesystem() as folder: - if exists: - Path(folder, 'img.tif').write_bytes(b'01010') - - asset_download_command = [ - 'asset-download', - item_type, - item_id, - asset_type, - f'--directory={Path(folder)}', - '--filename', - 'img.tif' - ] - if overwrite: - asset_download_command.append('--overwrite') + folder = tmp_path + if exists: + Path(folder, 'img.tif').write_bytes(b'01010') - result = invoke(asset_download_command, runner=runner) - assert result.exit_code == 0 + asset_download_command = [ + 'asset-download', + item_type, + item_id, + asset_type, + f'--directory={Path(folder)}', + '--filename', + 'img.tif' + ] + if overwrite: + asset_download_command.append('--overwrite') + + result = invoke(asset_download_command, runner=runner) + assert result.exit_code == 0 - path = Path(folder, 'img.tif') + path = Path(folder, 'img.tif') - assert path.name == 'img.tif' - assert path.is_file() + assert path.name == 'img.tif' + assert path.is_file() - if exists and not overwrite: - assert len(path.read_bytes()) == 5 - assert len(result.output) == 0 - else: - assert len(path.read_bytes()) == 527 - assert path.name in result.output + if exists and not overwrite: + assert len(path.read_bytes()) == 5 + assert len(result.output) == 0 + else: + assert len(path.read_bytes()) == 527 + assert path.name in result.output @respx.mock diff --git a/tests/integration/test_mosaics_api.py b/tests/integration/test_mosaics_api.py index a6a9a05aa..19fb37fc8 100644 --- a/tests/integration/test_mosaics_api.py +++ b/tests/integration/test_mosaics_api.py @@ -34,8 +34,8 @@ async def wrapper(*args, **kwargs): # @pytest.mark.skip @pytest.mark.parametrize( "tc", [pytest.param(tc, id=tc.id) for tc in test_mosaics_cli.test_cases]) -def test_api(tc): +def test_api(tc, cwd_tmp_path): api = async_wrap(MosaicsAPI) with patch('planet.cli.mosaics.MosaicsClient', api): - test_mosaics_cli.run_test(tc) + test_mosaics_cli.run_test(tc, cwd_tmp_path) api._pool.shutdown() diff --git a/tests/integration/test_mosaics_cli.py b/tests/integration/test_mosaics_cli.py index 1b28ca2a8..2a14f4657 100644 --- a/tests/integration/test_mosaics_cli.py +++ b/tests/integration/test_mosaics_cli.py @@ -376,32 +376,31 @@ class CLITestCase: @pytest.mark.parametrize("tc", [pytest.param(tc, id=tc.id) for tc in test_cases]) -def test_cli(tc: CLITestCase): - run_test(tc) +def test_cli(tc: CLITestCase, cwd_tmp_path): + run_test(tc, cwd_tmp_path) @respx.mock -def run_test(tc: CLITestCase): +def run_test(tc: CLITestCase, folder): runner = CliRunner() - with runner.isolated_filesystem() as folder: - for r in tc.requests: - r() - - args = ["mosaics", "-u", baseurl] + tc.command + tc.args - result = runner.invoke(cli.main, args=args) - # result.exception may be SystemExit which we want to ignore - # but if we don't raise a "true error" exception, there's no - # stack trace, making it difficult to diagnose - if result.exception and tc.exit_code == 0: - raise result.exception - assert result.exit_code == tc.exit_code, result.output - if tc.output: - try: - # error output (always?) not JSON - output = json.loads(result.output) - except json.JSONDecodeError: - output = result.output - assert output == tc.output - if tc.expect_files: - for f in tc.expect_files: - assert Path(folder, f).exists(), f + for r in tc.requests: + r() + + args = ["mosaics", "-u", baseurl] + tc.command + tc.args + result = runner.invoke(cli.main, args=args) + # result.exception may be SystemExit which we want to ignore + # but if we don't raise a "true error" exception, there's no + # stack trace, making it difficult to diagnose + if result.exception and tc.exit_code == 0: + raise result.exception + assert result.exit_code == tc.exit_code, result.output + if tc.output: + try: + # error output (always?) not JSON + output = json.loads(result.output) + except json.JSONDecodeError: + output = result.output + assert output == tc.output + if tc.expect_files: + for f in tc.expect_files: + assert Path(folder, f).exists(), f diff --git a/tests/integration/test_orders_cli.py b/tests/integration/test_orders_cli.py index e6920a4e6..24570ee9f 100644 --- a/tests/integration/test_orders_cli.py +++ b/tests/integration/test_orders_cli.py @@ -16,7 +16,6 @@ import hashlib from http import HTTPStatus import json -from pathlib import Path from unittest.mock import Mock from click.testing import CliRunner @@ -359,81 +358,96 @@ def _func(): @respx.mock -def test_cli_orders_download_default(invoke, mock_download_response, oid): +def test_cli_orders_download_default(invoke, + mock_download_response, + oid, + tmp_path, + monkeypatch): mock_download_response() + monkeypatch.chdir(tmp_path) runner = CliRunner() - with runner.isolated_filesystem() as folder: - result = invoke(['download', oid], runner=runner) - assert result.exit_code == 0 + result = invoke(['download', oid], runner=runner) + assert result.exit_code == 0 - # basic check of progress reporting - assert 'm1.json' in result.output + # basic check of progress reporting + assert 'm1.json' in result.output - # Check that the files were downloaded and have the correct contents - with open(Path(folder) / f'{oid}/itemtype/m1.json') as f: - assert json.load(f) == {'key': 'value'} - with open(Path(folder) / f'{oid}/itemtype/m2.json') as f: - assert json.load(f) == {'key2': 'value2'} + # Check that the files were downloaded and have the correct contents + with open(tmp_path / f'{oid}/itemtype/m1.json') as f: + assert json.load(f) == {'key': 'value'} + with open(tmp_path / f'{oid}/itemtype/m2.json') as f: + assert json.load(f) == {'key2': 'value2'} @respx.mock -def test_cli_orders_download_checksum(invoke, mock_download_response, oid): +def test_cli_orders_download_checksum(invoke, + mock_download_response, + oid, + tmp_path): """checksum is successful""" mock_download_response() runner = CliRunner() - with runner.isolated_filesystem(): - result = invoke(['download', oid, '--checksum=MD5'], runner=runner) - assert result.exit_code == 0 + result = invoke( + ['download', '--directory', str(tmp_path), oid, '--checksum=MD5'], + runner=runner) + assert result.exit_code == 0 @respx.mock -def test_cli_orders_download_dest(invoke, mock_download_response, oid): +def test_cli_orders_download_dest(invoke, + mock_download_response, + oid, + tmp_path): mock_download_response() + dest_dir = tmp_path / 'foobar' + dest_dir.mkdir() + runner = CliRunner() - with runner.isolated_filesystem() as folder: - dest_dir = Path(folder) / 'foobar' - dest_dir.mkdir() - result = invoke(['download', '--directory', 'foobar', oid], - runner=runner) - assert result.exit_code == 0 + result = invoke(['download', '--directory', str(dest_dir), oid], + runner=runner) + assert result.exit_code == 0 - # Check that the files were downloaded to the custom directory - with open(dest_dir / f'{oid}/itemtype/m1.json') as f: - assert json.load(f) == {'key': 'value'} + # Check that the files were downloaded to the custom directory + with open(dest_dir / f'{oid}/itemtype/m1.json') as f: + assert json.load(f) == {'key': 'value'} - with open(dest_dir / f'{oid}/itemtype/m2.json') as f: - assert json.load(f) == {'key2': 'value2'} + with open(dest_dir / f'{oid}/itemtype/m2.json') as f: + assert json.load(f) == {'key2': 'value2'} @respx.mock def test_cli_orders_download_overwrite(invoke, mock_download_response, oid, - write_to_tmp_json_file): + write_to_tmp_json_file, + tmp_path): mock_download_response() + filepath = tmp_path / f'{oid}/itemtype/m1.json' + filepath.parent.mkdir(parents=True) + filepath.write_text(json.dumps({'foo': 'bar'})) + runner = CliRunner() - with runner.isolated_filesystem() as folder: - filepath = Path(folder) / f'{oid}/itemtype/m1.json' - filepath.parent.mkdir(parents=True) - filepath.write_text(json.dumps({'foo': 'bar'})) - # check the file doesn't get overwritten by default - result = invoke(['download', oid], runner=runner) - assert result.exit_code == 0 + # check the file doesn't get overwritten by default + result = invoke(['download', '--directory', str(tmp_path), oid], + runner=runner) + assert result.exit_code == 0 - with open(filepath, 'r') as f: - assert json.load(f) == {'foo': 'bar'} + with open(filepath, 'r') as f: + assert json.load(f) == {'foo': 'bar'} - # check the file gets overwritten - result = invoke(['download', '--overwrite', oid], runner=runner) - assert result.exit_code == 0 + # check the file gets overwritten + result = invoke( + ['download', '--overwrite', '--directory', str(tmp_path), oid], + runner=runner) + assert result.exit_code == 0 - with open(filepath, 'r') as f: - assert json.load(f) == {'key': 'value'} + with open(filepath, 'r') as f: + assert json.load(f) == {'key': 'value'} @respx.mock diff --git a/tests/unit/test_cli_collect.py b/tests/unit/test_cli_collect.py index 0c25769e0..0085ac4a1 100644 --- a/tests/unit/test_cli_collect.py +++ b/tests/unit/test_cli_collect.py @@ -50,7 +50,7 @@ def test_cli_collect_stdin_features(feature_geojson): assert json.loads(result.output) == expected -def test_cli_collect_file(feature_geojson): +def test_cli_collect_file(feature_geojson, tmp_path): feature2 = feature_geojson.copy() feature2['properties'] = {'foo': 'bar'} values = [feature_geojson, feature2] @@ -58,12 +58,11 @@ def test_cli_collect_file(feature_geojson): runner = CliRunner() sequence = '\n'.join([json.dumps(v) for v in values]) - with runner.isolated_filesystem(): - with open('input.json', 'w') as f: - f.write(sequence) + input_file = tmp_path / 'input.json' + input_file.write_text(sequence) - result = runner.invoke(cli.main, ['collect', 'input.json']) + result = runner.invoke(cli.main, ['collect', str(input_file)]) - assert result.exit_code == 0 - expected = {'type': 'FeatureCollection', 'features': values} - assert json.loads(result.output) == expected + assert result.exit_code == 0 + expected = {'type': 'FeatureCollection', 'features': values} + assert json.loads(result.output) == expected