def setUp(self):
     super(ProvToolboxComparatorTestCase, self).setUp()
     self.comparator = ProvToolboxComparator()
     self.file1 = None
     self.file2 = None
     self.config = {
         ProvToolboxComparator.EXECUTABLE: "provconvert",
         ProvToolboxComparator.ARGUMENTS: " ".join(
             ["-infile", ProvToolboxComparator.FILE1, "-compare", ProvToolboxComparator.FILE2]
         ),
         ProvToolboxComparator.FORMATS: [standards.PROVN, standards.TRIG, standards.PROVX, standards.JSON],
     }
class ProvToolboxComparatorTestCase(unittest.TestCase):
    def setUp(self):
        super(ProvToolboxComparatorTestCase, self).setUp()
        self.comparator = ProvToolboxComparator()
        self.file1 = None
        self.file2 = None
        self.config = {
            ProvToolboxComparator.EXECUTABLE: "provconvert",
            ProvToolboxComparator.ARGUMENTS: " ".join(
                ["-infile", ProvToolboxComparator.FILE1, "-compare", ProvToolboxComparator.FILE2]
            ),
            ProvToolboxComparator.FORMATS: [standards.PROVN, standards.TRIG, standards.PROVX, standards.JSON],
        }

    def tearDown(self):
        super(ProvToolboxComparatorTestCase, self).tearDown()
        for tmp in [self.file1, self.file2]:
            if tmp != None and os.path.isfile(tmp):
                os.remove(tmp)

    def test_init(self):
        self.assertEqual("", self.comparator.executable)
        self.assertEqual([], self.comparator.arguments)
        self.assertEqual([], self.comparator.formats)

    def test_configure(self):
        self.comparator.configure(self.config)
        self.assertEqual(self.config[ProvToolboxComparator.EXECUTABLE].split(), self.comparator.executable)
        self.assertEqual(self.config[ProvToolboxComparator.ARGUMENTS].split(), self.comparator.arguments)
        self.assertEqual(self.config[ProvToolboxComparator.FORMATS], self.comparator.formats)

    def test_configure_no_file1(self):
        self.config[ProvToolboxComparator.ARGUMENTS] = " ".join(["-compare", ProvToolboxComparator.FILE2])
        with self.assertRaises(ConfigError):
            self.comparator.configure(self.config)

    def test_configure_no_file2(self):
        self.config[ProvToolboxComparator.ARGUMENTS] = " ".join(["-compare", ProvToolboxComparator.FILE2])

        with self.assertRaises(ConfigError):
            self.comparator.configure(self.config)

    def test_compare(self):
        self.comparator.configure(self.config)
        _, self.file1 = tempfile.mkstemp(suffix="." + standards.JSON)
        _, self.file2 = tempfile.mkstemp(suffix="." + standards.JSON)
        with open(self.file1, "a") as f1:
            f1.write("{}")
        with open(self.file2, "a") as f2:
            f2.write("{}")
        self.assertTrue(self.comparator.compare(self.file1, self.file2))

    def test_compare_non_equivalent(self):
        self.comparator.configure(self.config)
        (_, self.file1) = tempfile.mkstemp(suffix="." + standards.JSON)
        (_, self.file2) = tempfile.mkstemp(suffix="." + standards.JSON)
        with open(self.file1, "a") as f1:
            f1.write("FILE")
        with open(self.file2, "a") as f2:
            f2.write("{}")
        self.assertFalse(self.comparator.compare(self.file1, self.file2))

    def test_compare_oserror(self):
        self.config[ProvToolboxComparator.EXECUTABLE] = "/nosuchexecutable"
        self.comparator.configure(self.config)
        (_, self.file1) = tempfile.mkstemp(suffix="." + standards.JSON)
        (_, self.file2) = tempfile.mkstemp(suffix="." + standards.JSON)
        with self.assertRaises(OSError):
            self.comparator.compare(self.file1, self.file2)

    def test_compare_missing_file1(self):
        self.comparator.configure(self.config)
        self.file1 = "nosuchfile." + standards.JSON
        (_, self.file2) = tempfile.mkstemp(suffix="." + standards.JSON)
        with self.assertRaises(ComparisonError):
            self.comparator.compare(self.file1, self.file2)

    def test_compare_missing_file2(self):
        self.comparator.configure(self.config)
        (_, self.file1) = tempfile.mkstemp(suffix="." + standards.JSON)
        self.file2 = "nosuchfile." + standards.JSON
        with self.assertRaises(ComparisonError):
            self.comparator.compare(self.file1, self.file2)

    def test_compare_invalid_format1(self):
        self.comparator.configure(self.config)
        (_, self.file1) = tempfile.mkstemp(suffix=".nosuchformat")
        (_, self.file2) = tempfile.mkstemp(suffix="." + standards.JSON)
        with self.assertRaises(ComparisonError):
            self.comparator.compare(self.file1, self.file2)

    def test_compare_invalid_format2(self):
        self.comparator.configure(self.config)
        (_, self.file1) = tempfile.mkstemp(suffix="." + standards.JSON)
        (_, self.file2) = tempfile.mkstemp(suffix=".nosuchformat")
        with self.assertRaises(ComparisonError):
            self.comparator.compare(self.file1, self.file2)