googleapis / sample-tester

Tool for testing semantically equivalent samples in multiple languages and environments
Apache License 2.0
10 stars 12 forks source link

`code:` isn't evaluated from the correct directory #124

Open beccasaurus opened 5 years ago

beccasaurus commented 5 years ago

Steps to reproduce:

Expected result:

Actual:

If I cd around my system, the value of os.getcwd() changes as I cd where I invoke sample-tester from. This should remain static and should always be the working directory specified in the test's chdir:


Use case example (how I ran into this)

This means that my tests fail unless I cd into a very particular directory (which is already configured as the chdir:)

From texttospeech/ library root, where sample tests are typically run from:

$ sample-tester samples/v1/test/*

RUNNING: Test environment: ""
  RUNNING: Test suite: "Synthesizing Voice Audio"
    FAILED: Test case: "Converting text to synthetic voice audio"
      | 
      | ### Test case SETUP
      | 
      | ### Test case TEST
      | 
      | # Calling: python3 ./v1/tts_synthesize_text.py 
      | # FAILED ASSERTION: output.mp3 file expected but not found
      | 
      | ### Test case TEARDOWN
      | 

Tests failed

Change directory into texttospeech/samples/ (shouldn't need to be in this directory)

$ sample-tester v1/test/*

RUNNING: Test environment: ""
  RUNNING: Test suite: "Synthesizing Voice Audio"
    PASSED: Test case: "Converting text to synthetic voice audio"

Tests passed

Implementation thought:

vchudnov-g commented 5 years ago

It sounds like the issue is that, as captured in the doc, chdir changes to the specified directory before executing the sample, not before running the test. chdir is a property of the sample, after all, in the manifest file. So with this behavior, it's expected that printing os.getcwd() would give you the directory from which the sample-tester is run, whereas call() should switch to the chdir directory before calling the sample. (If you had the sample printout os.getcwd(), it should always give the chdir directory if one was provided.)

Sounds like what's needed is a way to have the test plan extract a particular sample's chdir and then cd to it.

beccasaurus commented 5 years ago

+1

Where "have the test plan extract a particular sample's chdir and then cd to it." can use a language built-in to switch the running context safely to that dir (and switch back to the main dir after code evaluation)

beccasaurus commented 5 years ago

If there's no idiomatic block-ish way to eval a bit of code chdir'd, looks like the recommended approach is just to try/finally

program_dir = os.getcwd()
sample_chdir = sample.chdir

try:
  os.chdir(sample_chdir)
  # eval the things
finally:
  os.chdir(program_dir)