def test_gather_sources_and_dependencies(): from tests.dependency_example import some_func main, sources, deps = gather_sources_and_dependencies( some_func.__globals__) assert isinstance(main, Source) assert isinstance(sources, set) assert isinstance(deps, set) assert main == Source.create( os.path.join(TEST_DIRECTORY, "dependency_example.py")) expected_sources = { Source.create(os.path.join(TEST_DIRECTORY, "__init__.py")), Source.create(os.path.join(TEST_DIRECTORY, "dependency_example.py")), Source.create(os.path.join(TEST_DIRECTORY, "foo", "__init__.py")), Source.create(os.path.join(TEST_DIRECTORY, "foo", "bar.py")), } assert sources == expected_sources assert PackageDependency.create(pytest) in deps assert PackageDependency.create(mock) in deps # If numpy is installed on the test system it will automatically be added # as an additional dependency, so test for that: if opt.has_numpy: assert PackageDependency.create(opt.np) in deps assert len(deps) == 3 else: assert len(deps) == 2
def test_gather_sources_and_dependencies(discover_sources, expected_sources): from tests.dependency_example import some_func from sacred import SETTINGS SETTINGS.DISCOVER_SOURCES = discover_sources main, sources, deps = gather_sources_and_dependencies( some_func.__globals__, save_git_info=False) assert isinstance(main, Source) assert isinstance(sources, set) assert isinstance(deps, set) assert main == Source.create( os.path.join(TEST_DIRECTORY, "dependency_example.py")) assert sources == expected_sources assert PackageDependency.create(pytest) in deps assert PackageDependency.create(mock) in deps # If numpy is installed on the test system it will automatically be added # as an additional dependency, so test for that: if opt.has_numpy: assert PackageDependency.create(opt.np) in deps assert len(deps) == 3 else: assert len(deps) == 2 # Reset to default to prevent side-effects SETTINGS.DISCOVER_SOURCES = "imported"
def test_add_source_file(ing): handle, f_name = tempfile.mkstemp(suffix='.py') f = os.fdopen(handle, "w") f.write("print('Hello World')") f.close() ing.add_source_file(f_name) assert Source.create(f_name) in ing.sources os.remove(f_name)
def add_source_file(self, filename): """ Add a file as source dependency to this experiment/ingredient. :param filename: filename of the source to be added as dependency :type filename: str """ self.sources.add(Source.create(filename))
def test_gather_sources_and_dependencies(): from tests.dependency_example import some_func sources, deps = gather_sources_and_dependencies(some_func.__globals__) assert isinstance(sources, set) assert isinstance(deps, set) expected_sources = { Source.create('tests/__init__.py'), Source.create('tests/dependency_example.py'), Source.create('tests/foo/__init__.py'), Source.create('tests/foo/bar.py') } assert sources == expected_sources assert PackageDependency.create(pytest) in deps assert PackageDependency.create(mock) in deps # If numpy is installed on the test system it will automatically be added # as an additional dependency, so test for that: if opt.has_numpy: assert PackageDependency.create(opt.np) in deps assert len(deps) == 3 else: assert len(deps) == 2
def test_custom_base_dir(): from tests.basedir.my_experiment import some_func base_dir = os.path.abspath('tests') main, sources, deps = gather_sources_and_dependencies(some_func.__globals__, base_dir) assert isinstance(main, Source) assert isinstance(sources, set) assert isinstance(deps, set) assert main == Source.create('tests/basedir/my_experiment.py') expected_sources = { Source.create('tests/__init__.py'), Source.create('tests/basedir/__init__.py'), Source.create('tests/basedir/my_experiment.py'), Source.create('tests/foo/__init__.py'), Source.create('tests/foo/bar.py') } assert sources == expected_sources
def test_custom_base_dir(): from tests.basedir.my_experiment import some_func main, sources, deps = gather_sources_and_dependencies( some_func.__globals__, TEST_DIRECTORY) assert isinstance(main, Source) assert isinstance(sources, set) assert isinstance(deps, set) assert main == Source.create( os.path.join(TEST_DIRECTORY, 'basedir', 'my_experiment.py')) expected_sources = { Source.create(os.path.join(TEST_DIRECTORY, '__init__.py')), Source.create(os.path.join(TEST_DIRECTORY, 'basedir', '__init__.py')), Source.create( os.path.join(TEST_DIRECTORY, 'basedir', 'my_experiment.py')), Source.create(os.path.join(TEST_DIRECTORY, 'foo', '__init__.py')), Source.create(os.path.join(TEST_DIRECTORY, 'foo', 'bar.py')) } assert sources == expected_sources
def test_source_repr(): s = Source.create(EXAMPLE_SOURCE) assert repr(s) == "<Source: {}>".format(os.path.abspath(EXAMPLE_SOURCE))
def test_source_create_py(): s = Source.create(EXAMPLE_SOURCE) assert s.filename == os.path.abspath(EXAMPLE_SOURCE) assert s.digest == EXAMPLE_DIGEST
def test_create_ingredient(ing): assert ing.path == 'tickle' assert ing.doc == __doc__ assert Source.create(__file__) in ing.sources
pd.fill_missing_version() assert pd.version == pytest.__version__ def test_package_dependency_repr(): pd = PackageDependency("pytest", "12.4") assert repr(pd) == "<PackageDependency: pytest=12.4>" @pytest.mark.parametrize( "discover_sources, expected_sources", [ ( "imported", { Source.create(os.path.join(TEST_DIRECTORY, "__init__.py")), Source.create( os.path.join(TEST_DIRECTORY, "dependency_example.py")), Source.create( os.path.join(TEST_DIRECTORY, "foo", "__init__.py")), Source.create(os.path.join(TEST_DIRECTORY, "foo", "bar.py")), }, ), ( "dir", { # This list would be too long to explicitly insert here Source.create(str(path.resolve())) for path in Path(TEST_DIRECTORY).rglob("*.py") }, ),
def test_source_create_empty(): with pytest.raises(ValueError): Source.create('')
def test_source_create_non_existing(): with pytest.raises(ValueError): Source.create('doesnotexist.py')
def test_source_create_empty(): with pytest.raises(ValueError): Source.create("")
def test_source_create_non_existing(): with pytest.raises(ValueError): Source.create("doesnotexist.py")
def test_source_to_tuple(): s = Source.create(EXAMPLE_SOURCE) assert s.to_tuple() == (os.path.abspath(EXAMPLE_SOURCE), EXAMPLE_DIGEST)
def test_source_to_json(): s = Source.create(EXAMPLE_SOURCE) assert s.to_json() == (os.path.abspath(EXAMPLE_SOURCE), EXAMPLE_DIGEST)