def test_removed_barrel_not_in_cache(self): fake_cache = StringIO(self.__build_fake_cache(2)) mock_config = Mock(Config) attr = {"open_file.side_effect": lambda name, mode, cb: cb(fake_cache)} mock_config.configure_mock(**attr) mock_hasher = Mock(FileHasher) hash_attr = {"match.return_value": True} mock_hasher.configure_mock(**hash_attr) config = { "exists.side_effect": lambda x: { "test0.barrel": False }.get(x, True) } with patch("os.path", **config): cache = Cache(mock_config, mock_hasher) dep1Test = Dependency("Depend0") dep1Test.set_version("0.1.2") dep2Test = Dependency("Depend1") dep2Test.set_version("0.1.2") self.assertIn(dep2Test, cache) self.assertNotIn(dep1Test, cache)
def __download_dependency_assets(self, dep: Dependency) -> None: try: asset = self.__downloader.download_barrel(dep) logging.info( "Downloaded barrel {barrel} from release {tag}".format( barrel=asset.name, tag=asset.version)) except Error as e: logging.error( "Failed to download barrel for {dep}:{version} - {msg}".format( dep=dep.package_name, version=dep.version, msg=e.message)) return barrel_name = os.path.join(self.__project.config.barrel_dir, asset.name) self.__project.config.open_file(barrel_name, "wb", lambda f: f.write(asset.content)) dep.set_barrel_name(barrel_name)
def __initialize_dependency(self, package_name: str) -> None: new_dep = Dependency(package_name) new_dep.set_version(self.manifest.get_required_version(package_name)) new_dep.set_repo(self.packages.get_repo_for_package(package_name)) if new_dep in self.__cache: new_dep.set_barrel_name( self.__cache.get_barrel_for_package(new_dep)) self.__add_dependency(new_dep)
def test_cache_reads_json_cache_file(self): fake_cache = StringIO(self.__build_fake_cache()) mock_config = Mock(Config) attr = {"open_file.side_effect": lambda name, mode, cb: cb(fake_cache)} mock_config.configure_mock(**attr) mock_hasher = Mock(FileHasher) hash_attr = {"match.return_value": True} mock_hasher.configure_mock(**hash_attr) config = {"exists.return_value": True} with patch("os.path", **config): cache = Cache(mock_config, mock_hasher) depTest = Dependency("Depend") depTest.set_version("0.1.2") self.assertTrue(depTest in cache)
def test_can_overwrite_dependency(self): fake_cache = StringIO(self.__build_fake_cache()) mock_config = Mock(Config) attr = {"open_file.side_effect": lambda name, mode, cb: cb(fake_cache)} mock_config.configure_mock(**attr) mock_hasher = Mock(FileHasher) hash_attr = {"hash_file.return_value": "0123"} mock_hasher.configure_mock(**hash_attr) config = {"exists.return_value": True} with patch("os.path", **config): cache = Cache(mock_config, mock_hasher) depTest = Dependency("Depend") depTest.set_version("0.1.2") self.assertIn(depTest, cache) self.assertNotEqual("test3.barrel", cache.get_barrel_for_package(depTest)) depTest.set_barrel_name("test3.barrel") cache.add_dependency(depTest) self.assertEqual("test3.barrel", cache.get_barrel_for_package(depTest))
def test_cache_initializes_empty_if_cache_doesnt_exist(self): mock_config = Mock(Config) config = {"exists.return_value": False} with patch("os.path", **config): cache = Cache(mock_config) self.assertIsNotNone(cache) depTest = Dependency("Depend2") depTest.set_version("0.1.2") depTest.set_barrel_name("test.barrel") self.assertNotIn(depTest, cache)
def test_can_add_dependency(self): mock_config = Mock(Config) mock_hasher = Mock(FileHasher) hash_attr = {"hash_file.return_value": "0123"} mock_hasher.configure_mock(**hash_attr) config = {"exists.return_value": False} with patch("os.path", **config): cache = Cache(mock_config, mock_hasher) depTest = Dependency("Depend2") depTest.set_version("0.1.2") depTest.set_barrel_name("test.barrel") cache.add_dependency(depTest) self.assertIn(depTest, cache)
def test_dependency_ne_with_different_repo(self): dep = Dependency("Depend") dep.set_version("1.2.3") dep.set_repo("owner/Depend") dep2 = Dependency("Depend") dep2.set_version("1.2.3") dep2.set_repo("owner2/Depend") self.assertNotEqual(dep, dep2)
def test_dependency_eq_with_other(self): dep = Dependency("Depend") dep.set_version("1.2.3") dep.set_repo("owner/Depend") dep2 = Dependency("Depend") dep2.set_version("1.2.3") dep2.set_repo("owner/Depend") self.assertEqual(dep, dep2)
def test_dependency_can_get_barrel_name(self): dep = Dependency("Depend") dep.set_barrel_name("x.barrel") self.assertEqual("x.barrel", dep.barrel_name)
def test_dependency_can_get_repo(self): dep = Dependency("Depend") dep.set_repo("owner/Depend") self.assertEqual("owner/Depend", dep.repo)
def test_dependency_can_get_version(self): dep = Dependency("Depend") dep.set_version("1.2.3") self.assertEqual("1.2.3", str(dep.version))
def test_dependency_name_is_correct(self): dep = Dependency("Depend") self.assertEqual("Depend", dep.package_name)
def test_dependency_eq_with_different_barrel_name(self): dep = Dependency("Depend") dep.set_version("1.2.3") dep.set_repo("owner/Depend") dep.set_barrel_name("x.barrel") dep2 = Dependency("Depend") dep2.set_version("1.2.3") dep2.set_repo("owner/Depend") dep.set_barrel_name("y.barrel") self.assertEqual(dep, dep2)