Пример #1
0
class TestSubcommandUpdate(SubcommandTestHelper):
    """Update subcommand test suite."""

    def setUp(self):
        """Setup test suite."""
        self.subcommand = Update()
        self.subcommand_str = "update"
        super(TestSubcommandUpdate, self).setUp()

        config_data = {"workspaces": {"yoda": {"path": "/yoda", "repositories": {"yoda": "/project/yoda"}}}}

        self.config.update(config_data)

    def test_parse_update(self):
        """Test parse update subcommand."""
        self.assert_subcommand_parsing(["update", "ws1/repo1"], {"subcommand": "update", "name": "ws1/repo1"})

    def test_parse_update_all(self):
        """Test update all workspaces."""
        self.assert_subcommand_parsing(["update", "--all"], {"subcommand": "update", "all": True})

    def test_parse_update_raises_error(self):
        """
        Test parse update subcommand raises error when --all specified
        with workspace or repository name.
        """
        self.assert_subcommand_parsing_raises_error(["update", "--all", "ws1/repo1"], SystemExit)

    def test_exec_update_workspace_only(self):
        """Test exec update subcommand."""
        args = Mock()
        args.name = "foo/bar"

        mock_path = {"foo/bar": "/tmp/foo/bar"}

        self.subcommand.print_update = Mock()

        with patch("yoda.subcommand.update.find_path", return_value=mock_path) as patch_fp:
            self.subcommand.execute(args)
            patch_fp.assert_called_once_with("foo/bar", self.subcommand.config)
            self.subcommand.print_update.assert_called_once_with("foo/bar", "/tmp/foo/bar")

    def test_exec_update_all_workspaces(self):
        """Test exec with all workspaces."""
        args = Mock()
        args.name = None
        args.all = True

        mock_path = {"foo/bar": "/tmp/foo/bar"}

        self.subcommand.print_update = Mock()

        with patch("yoda.subcommand.update.find_path", return_value=mock_path):
            self.subcommand.execute(args)

    def test_exec_update_no_matches(self):
        """Test exec update subcommand with no matches."""
        args = Mock()
        args.name = "foobar"

        self.subcommand.print_update = Mock()
        with patch("yoda.subcommand.update.find_path", return_value={}):
            with LogCapture() as lcap:
                self.assertFalse(self.subcommand.execute(args))

        lcap.check(("yoda.subcommand.update", "ERROR", "No matches for `foobar`"))

    def test_print_update(self):
        """Test print_update."""
        with patch("yoda.subcommand.update.Repository"):
            with LogCapture() as lcap:
                self.subcommand.print_update("foo", "bar")

        lcap.check(("yoda.subcommand.update", "INFO", "\033[32m=> [foo] bar\033[0m"))

    def test_print_update_log_error_message(self):
        """Test that print_update logs the error message."""
        with LogCapture() as lcap:
            self.subcommand.print_update("foo", "/path/doesnot/exists")

        lcap.check(
            ("yoda.subcommand.update", "INFO", "\033[32m=> [foo] /path/doesnot/exists\033[0m"),
            ("yoda.subcommand.update", "ERROR", "Path doesn't exists or isn't a directory " "(/path/doesnot/exists)\n"),
        )
Пример #2
0
class TestSubcommandUpdate(unittest.TestCase):
    """Update subcommand test suite."""
    sandbox = None
    config = None
    parser = None
    subparser = None
    update = None

    def setUp(self):
        """Setup test suite."""
        self.parser = argparse.ArgumentParser(prog="yoda_test")
        self.subparser = self.parser.add_subparsers(dest="subcommand_test")

        self.update = Update()
        config_data = {
            "workspaces": {
                "yoda": {
                    "path": "/yoda",
                    "repositories": {
                        "yoda": "/project/yoda"
                    }
                }
            }
        }

        self.sandbox = Sandbox()
        self.config = Config(self.sandbox.path + "/config")
        self.config.update(config_data)
        self.update.setup("update", self.config, self.subparser)

    def tearDown(self):
        """Tear down test suite."""
        self.sandbox.destroy()
        self.parser = None
        self.update = None

    def test_parse_update(self):
        """Test parse update subcommand."""
        self.update.parse()

        args = self.parser.parse_args(["update", "ws1/repo1"])

        self.assertEqual("update", args.subcommand_test)
        self.assertEqual("ws1/repo1", args.name)

        args = self.parser.parse_args(["update", "--all"])

        self.assertEqual("update", args.subcommand_test)
        self.assertEqual(True, args.all)

        self.assertRaises(
            SystemExit,
            self.parser.parse_args, ["update", "--all", "ws1/repo1"]
        )

    def test_exec_update_workspace_only(self):
        """Test exec update subcommand."""
        args = Mock()
        args.name = "foo/bar"

        mock_path = {"foo/bar": "/tmp/foo/bar"}

        self.update.print_update = Mock()

        with patch("yoda.subcommand.update.find_path",
                   return_value=mock_path) as patch_fp:
            self.update.execute(args)
            patch_fp.assert_called_once_with("foo/bar", self.update.config)
            self.update.print_update.assert_called_once_with(
                "foo/bar", "/tmp/foo/bar")

    def test_exec_update_all_workspaces(self):
        """Test exec with all workspaces."""
        args = Mock()
        args.name = None
        args.all = True

        mock_path = {"foo/bar": "/tmp/foo/bar"}

        self.update.print_update = Mock()

        with patch("yoda.subcommand.update.find_path",
                   return_value=mock_path):
            self.update.execute(args)

    def test_exec_update_no_matches(self):
        """Test exec update subcommand with no matches."""
        args = Mock()
        args.name = "foobar"

        self.update.logger = Mock()
        self.update.print_update = Mock()

        with patch("yoda.subcommand.update.find_path", return_value={}):
            self.assertFalse(self.update.execute(args))
            self.update.logger.error.assert_called_once_with(
                "No matches for `foobar`")

    def test_print_update(self):
        """Test print_update."""
        self.update.logger = Mock()
        with patch("yoda.subcommand.update.Repository"):
            self.update.print_update("foo", "bar")
            self.update.logger.info.assert_has_calls(
                [call("\033[32m=> [foo] bar\033[0m")])