예제 #1
0
    def test_path_option(self):
        option = PathOption("test-directory", ".", u"Test Description", type=u"dir")
        self.assertEqual(option.value, os.getcwd())
        self.assertEqual(option.name, "test-directory")
        self.assertEqual(option.description, u"Test Description")
        self.assertEqual(option.type, u"dir")

        # re-set value
        option.set(self.SAMPLES_DIR)
        self.assertEqual(option.value, self.SAMPLES_DIR)

        # set to int
        expected = u"Option test-directory must be an existing directory \(current value: '1234'\)"
        with self.assertRaisesRegex(RuleOptionError, expected):
            option.set(1234)

        # set to non-existing directory
        non_existing_path = os.path.join(u"/föo", u"bar")
        expected = u"Option test-directory must be an existing directory \(current value: '{0}'\)"
        with self.assertRaisesRegex(RuleOptionError, expected.format(non_existing_path)):
            option.set(non_existing_path)

        # set to a file, should raise exception since option.type = dir
        sample_path = self.get_sample_path(os.path.join("commit_message", "sample1"))
        expected = u"Option test-directory must be an existing directory \(current value: '{0}'\)".format(
            re.escape(sample_path))
        with self.assertRaisesRegex(RuleOptionError, expected):
            option.set(sample_path)

        # set option.type = file, file should now be accepted, directories not
        option.type = u"file"
        option.set(sample_path)
        self.assertEqual(option.value, sample_path)
        expected = u"Option test-directory must be an existing file \(current value: '{0}'\)".format(
            re.escape(self.get_sample_path()))
        with self.assertRaisesRegex(RuleOptionError, expected):
            option.set(self.get_sample_path())

        # set option.type = both, files and directories should now be accepted
        option.type = u"both"
        option.set(sample_path)
        self.assertEqual(option.value, sample_path)
        option.set(self.get_sample_path())
        self.assertEqual(option.value, self.get_sample_path())

        # Expect exception if path type is invalid
        option.type = u'föo'
        expected = u"Option test-directory type must be one of: 'file', 'dir', 'both' \(current: 'föo'\)"
        with self.assertRaisesRegex(RuleOptionError, expected):
            option.set("haha")
예제 #2
0
    def test_path_option(self):
        option = PathOption("tëst-directory",
                            ".",
                            "Tëst Description",
                            type="dir")
        self.assertEqual(option.name, "tëst-directory")
        self.assertEqual(option.description, "Tëst Description")
        self.assertEqual(option.value, os.getcwd())
        self.assertEqual(option.type, "dir")

        # re-set value
        option.set(self.SAMPLES_DIR)
        self.assertEqual(option.value, self.SAMPLES_DIR)

        # set to None
        option.set(None)
        self.assertIsNone(option.value)

        # set to int
        expected = "Option tëst-directory must be an existing directory (current value: '1234')"
        with self.assertRaisesMessage(RuleOptionError, expected):
            option.set(1234)

        # set to non-existing directory
        non_existing_path = os.path.join("/föo", "bar")
        expected = f"Option tëst-directory must be an existing directory (current value: '{non_existing_path}')"
        with self.assertRaisesMessage(RuleOptionError, expected):
            option.set(non_existing_path)

        # set to a file, should raise exception since option.type = dir
        sample_path = self.get_sample_path(
            os.path.join("commit_message", "sample1"))
        expected = f"Option tëst-directory must be an existing directory (current value: '{sample_path}')"
        with self.assertRaisesMessage(RuleOptionError, expected):
            option.set(sample_path)

        # set option.type = file, file should now be accepted, directories not
        option.type = "file"
        option.set(sample_path)
        self.assertEqual(option.value, sample_path)
        expected = f"Option tëst-directory must be an existing file (current value: '{self.get_sample_path()}')"
        with self.assertRaisesMessage(RuleOptionError, expected):
            option.set(self.get_sample_path())

        # set option.type = both, files and directories should now be accepted
        option.type = "both"
        option.set(sample_path)
        self.assertEqual(option.value, sample_path)
        option.set(self.get_sample_path())
        self.assertEqual(option.value, self.get_sample_path())

        # Expect exception if path type is invalid
        option.type = u'föo'
        expected = "Option tëst-directory type must be one of: 'file', 'dir', 'both' (current: 'föo')"
        with self.assertRaisesMessage(RuleOptionError, expected):
            option.set("haha")