|
| 1 | +import builtins |
1 | 2 | import os |
2 | | -from pathlib import Path |
| 3 | +import random |
| 4 | +import string |
3 | 5 |
|
4 | 6 | import pytest |
5 | 7 | import test_utils |
6 | 8 | from base import BaseTest |
7 | 9 |
|
8 | 10 | from datashuttle.configs.canonical_configs import get_broad_datatypes |
| 11 | +from datashuttle.utils import rclone |
9 | 12 |
|
10 | 13 |
|
11 | 14 | @pytest.mark.skipif(os.getenv("CI") is None, reason="Only runs in CI") |
12 | 15 | class TestGoogleDriveGithubCI(BaseTest): |
13 | 16 |
|
14 | | - def test_google_drive_connection(self, project): |
15 | | - # ── 1 pull the secrets from env ─────────────────────────────── |
| 17 | + def test_google_drive_connection(self, no_cfg_project, tmp_path): |
| 18 | + |
| 19 | + central_path = ( |
| 20 | + f"test-id-{''.join(random.choices(string.digits, k=15))}" |
| 21 | + ) |
| 22 | + |
16 | 23 | root_id = os.environ["GDRIVE_ROOT_FOLDER_ID"] |
17 | 24 | sa_path = os.environ["GDRIVE_SERVICE_ACCOUNT_FILE"] |
18 | 25 |
|
19 | | - # ── 2 configure the project (no hard-coded ids/paths) ───────── |
20 | | - project.update_config_file( |
21 | | - local_path=str(Path.home() / "data"), # any temp location |
| 26 | + no_cfg_project.make_config_file( |
| 27 | + local_path=str(tmp_path), # any temp location TODO UPDATE |
22 | 28 | connection_method="gdrive", |
23 | | - central_path="testGDrive", |
| 29 | + central_path=central_path, |
24 | 30 | gdrive_root_folder_id=root_id, |
25 | 31 | gdrive_client_id=None, # keep None |
26 | 32 | ) |
27 | 33 |
|
28 | | - # ── 3 feed the SA-file path to the interactive prompt ───────── |
29 | 34 | state = {"first": True} |
30 | 35 |
|
31 | 36 | def mock_input(_: str) -> str: |
32 | 37 | if state["first"]: |
33 | 38 | state["first"] = False |
34 | | - return "n" # ← tells setup to use file, not auth-browser |
35 | | - return sa_path # ← absolute path written in the workflow |
36 | | - |
37 | | - import builtins |
| 39 | + return "n" |
| 40 | + return sa_path |
38 | 41 |
|
39 | 42 | original_input = builtins.input |
40 | 43 | builtins.input = mock_input |
41 | 44 |
|
42 | | - try: |
43 | | - project.setup_google_drive_connection() |
44 | | - finally: |
45 | | - builtins.input = original_input # always restore |
| 45 | + no_cfg_project.setup_google_drive_connection() |
| 46 | + |
| 47 | + builtins.input = original_input |
46 | 48 |
|
47 | | - # ── 4 run the usual checks ──────────────────────────────────── |
48 | 49 | subs, sessions = test_utils.get_default_sub_sessions_to_test() |
| 50 | + |
49 | 51 | test_utils.make_and_check_local_project_folders( |
50 | | - project, "rawdata", subs, sessions, get_broad_datatypes() |
| 52 | + no_cfg_project, "rawdata", subs, sessions, get_broad_datatypes() |
51 | 53 | ) |
52 | 54 |
|
53 | | - project.upload_entire_project() |
| 55 | + no_cfg_project.upload_entire_project() |
| 56 | + |
| 57 | + # get a list of files on gdrive and check they are as expected |
| 58 | + # assert the test id if its failed |
| 59 | + |
| 60 | + # only tidy up if as expected, otherwise we can leave the folder there to have a look |
| 61 | + # and delete manually later |
| 62 | + rclone.call_rclone( |
| 63 | + f"purge central_{no_cfg_project.project_name}_gdrive:{central_path}" |
| 64 | + ) |
0 commit comments