Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] cassette_library_dir from vcr_config is ignored when searching for extra cassettes #151

Open
LucHermitte opened this issue Sep 18, 2024 · 1 comment

Comments

@LucHermitte
Copy link

Describe the bug

When a test depends on extra shared cassettes, we cannot override the default vcr_cassette_dir through cassette_library_dir key returned from vcr_config fixture.

While vcr_cassette_dir is correctly used to set where the current cassette is read/written, it's ignored when searching for extra cassettes.

To Reproduce
A simplified Minimal (Not Working) Example would be:

# file: test_eof.py

@pytest.fixture(scope="module")
def vcr_config(baseline_dir):
    # baseline_dir being another fixture set elsewhere
    assert os.path.exists(baseline_dir)
    return {
            "cassette_library_dir"       : os.path.join(baseline_dir, 'cassettes/test_eof'),
            # I guess I should be able to use module.purebasename instead of hardcoding "test_eof"
    }

@pytest.mark.vcr("cop_access_token.yaml")
def test_sometest():
    Code that executes a request that I expect recorded in:
   -> ${BASELINE_DIR}/cassettes/test_eof/cop_access_token.yaml
    But the cassettes searched are:
    - $(current_test_dir)/cassettes/test_eof/cop_access_token.yaml  <-- not the path expected
    - and ${BASELINE_DIR}/cassettes/test_eof/test_sometest.yaml <- the current test

Expected behavior
The shared cassettes should be searched in vcr_config()["cassette_library_dir"]

Possible patch
Adding the following instruction in _vcr.use_cassette() seems enough to fix the issue.

    # just after >> merged_config = merge_kwargs(config, markers) <<
    vcr_cassette_dir = config.get('cassette_library_dir', vcr_cassette_dir)  # <<-- The proposed fix

Or maybe, it's vcr_cassette_dir fixture itself that needs the patching? I don't know what's better.

@pytest.fixture(scope="module")  # type: ignore
def vcr_cassette_dir(request: SubRequest) -> str:
    """Each test module has its own cassettes directory to avoid name collisions.

    For example each test module could have test function with the same names:
      - test_users.py:test_create
      - test_profiles.py:test_create
    """
+    config = request.getfixturevalue("vcr_config")
+    if "cassette_library_dir" in config:
+        return config["cassette_library_dir"]
    module = request.node.fspath  # current test file
    return os.path.join(module.dirname, "cassettes", module.purebasename)

Environment:

  • OS: ubuntu
  • Python version: 3.11
  • pytest-recording version: 0.13.2
  • pytest version: 8.0.2
@LucHermitte
Copy link
Author

My mistake, it seems we can simply override vcr_cassette_dir. It deserved to be documented though.

It's not much a bug then, but more an opportunity to improve documentation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants