def test_untap(self, cask, debug, verbose, tap): with pytest.raises(AssertionError) if cask else empty(): brew = Brew(cask, debug, verbose) args = self.__cmd() + ["untap", tap] args += self.__flags(debug, verbose) brew.untap(tap) sh.spawn.assert_called_with(args, debug=debug, check_output=False, env=brew.env)
def test_tap(self, cask, debug, verbose, tap, use_tap): with pytest.raises(AssertionError) if cask else empty(): brew = Brew(cask, debug, verbose) tap = tap if use_tap else None args = self.__cmd() + ["tap"] + ([tap] if tap is not None else []) args += self.__flags(debug, verbose) brew.tap(tap) sh.spawn.assert_called_with(args, debug=debug, check_output=not use_tap, env=brew.env)
def test_read_config(path, contents, fallback): def test_case(assertion, msg, mock=None, read_data=None): with patch("__builtin__.open", mock_open(mock, read_data)): assert assertion(), msg test_case(lambda: sh.read_config(path, fallback) == contents, "Should read valid YAML", read_data=yaml.dump(contents, default_flow_style=False)) with pytest.raises(IOError) if fallback is None else empty(): test_case(lambda: sh.read_config(path, fallback) == fallback, "Should use fallback if file is missing", MagicMock(side_effect=IOError(errno.ENOENT, ""))) with pytest.raises(ParserError): test_case( lambda: sh.read_config(path, fallback) is None, "Should raise error if data is invalid", read_data="[garbage}", )
def test_read_config(path, contents, fallback): def test_case(assertion, msg, mock=None, read_data=None): with patch("__builtin__.open", mock_open(mock, read_data)): assert assertion(), msg test_case( lambda: sh.read_config(path, fallback) == contents, "Should read valid YAML", read_data=yaml.dump(contents, default_flow_style=False) ) with pytest.raises(IOError) if fallback is None else empty(): test_case( lambda: sh.read_config(path, fallback) == fallback, "Should use fallback if file is missing", MagicMock(side_effect=IOError(errno.ENOENT, "")) ) with pytest.raises(ParserError): test_case( lambda: sh.read_config(path, fallback) is None, "Should raise error if data is invalid", read_data="[garbage}", )
def test_relink(self, tmpdir, debug, verbose, force): """ Tests that: 1. Target directories are created. 2. For each source in glob(key), mklink(src, expandtarget(src, target)) is called. 3. Previously-cached targets are removed. 4. Cache is updated with new targets. """ def generate_symlinks(): srcdir = tmpdir.join(random_str(min_length=1)) def symkey(directory, key): return str(directory.join(key).relto(srcdir)) def symvalue(directory, value): return str(directory.join(value)) + ( "/" if value.endswith("/") else "" ) outerdir = srcdir.join(random_str(min_length=1)) innerdir = outerdir.join(random_str(min_length=1)) targetdir = tmpdir.join(random_str(min_length=1)) ext = random_str(min_length=1, max_length=8) os.makedirs(str(innerdir)) for _ in range(random.randint(0, 10)): touch(str(innerdir.join("{0}.{1}".format(random_str(), ext)))) path = str(outerdir.join(random_str(min_length=1))) touch(path) return { symkey(outerdir, "*/*." + ext): symvalue(targetdir, "a/b/c/"), symkey(outerdir, "*/*." + ext): symvalue(targetdir, "a/b/c"), symkey(outerdir, path): symvalue(targetdir, "a/b/d"), } cider = Cider(False, debug, verbose, cider_dir=str(tmpdir)) cider.mklink = MagicMock(return_value=True) for srcglob, target in generate_symlinks().items(): invalid = not isdirname(target) and ("*" in srcglob or "?" in srcglob) old_targets = cider._cached_targets() # pylint:disable=W0212 cider.read_bootstrap = MagicMock(return_value={ "symlinks": {srcglob: target} }) with pytest.raises(SymlinkError) if invalid else empty(): new_targets = set(cider.relink(force)) for src in iglob(srcglob): cider.mklink.assert_called_with(src, cider.expandtarget( src, target )) assert os.path.isdir(os.path.dirname(target)) for dead_target in set(old_targets) - new_targets: assert not os.path.exists(dead_target) new_cache = cider._cached_targets() # pylint:disable=W0212 assert new_targets == set(new_cache).intersection(new_targets)
def test_relink(self, tmpdir, debug, verbose, force): """ Tests that: 1. Target directories are created. 2. For each source in glob(key), mklink(src, expandtarget(src, target)) is called. 3. Previously-cached targets are removed. 4. Cache is updated with new targets. """ def generate_symlinks(): srcdir = tmpdir.join(random_str(min_length=1)) def symkey(directory, key): return str(directory.join(key).relto(srcdir)) def symvalue(directory, value): return str(directory.join(value)) + ("/" if value.endswith("/") else "") outerdir = srcdir.join(random_str(min_length=1)) innerdir = outerdir.join(random_str(min_length=1)) targetdir = tmpdir.join(random_str(min_length=1)) ext = random_str(min_length=1, max_length=8) os.makedirs(str(innerdir)) for _ in range(random.randint(0, 10)): touch(str(innerdir.join("{0}.{1}".format(random_str(), ext)))) path = str(outerdir.join(random_str(min_length=1))) touch(path) return { symkey(outerdir, "*/*." + ext): symvalue(targetdir, "a/b/c/"), symkey(outerdir, "*/*." + ext): symvalue(targetdir, "a/b/c"), symkey(outerdir, path): symvalue(targetdir, "a/b/d"), } cider = Cider(False, debug, verbose, cider_dir=str(tmpdir), support_dir=str(tmpdir.join(".cache"))) cider.mklink = MagicMock(return_value=True) for srcglob, target in generate_symlinks().items(): invalid = not isdirname(target) and ("*" in srcglob or "?" in srcglob) old_targets = cider._cached_targets() # pylint:disable=W0212 cider.read_bootstrap = MagicMock( return_value={"symlinks": { srcglob: target }}) with pytest.raises(SymlinkError) if invalid else empty(): new_targets = set(cider.relink(force)) for src in iglob(srcglob): cider.mklink.assert_called_with( src, cider.expandtarget(src, target)) assert os.path.isdir(os.path.dirname(target)) for dead_target in set(old_targets) - new_targets: assert not os.path.exists(dead_target) new_cache = cider._cached_targets() # pylint:disable=W0212 assert new_targets == set(new_cache) & new_targets