def create(self):
     """
     Create the files necessary for this store
     :return: True if changes were made, false otherwise
     """
     return (create_dir(os.path.dirname(self.path)) or self.read_file()
             or create_file(self.path, self.contents))
 def create(self):
     """
     Create the files necessary for this store
     :return: True if changes were made, false otherwise
     """
     return create_dir(os.path.dirname(self.path)) or \
         self.read_file() or \
         create_file(self.path, self.contents)
    def test_create_file(self):
        temp_dir = tempfile.TemporaryDirectory()
        to_create = os.path.join(temp_dir.name, "my_cool_file.json")

        test_data = {"arbitrary_key": "arbitrary_value"}

        res = utils.create_file(to_create, test_data)
        self.assertTrue(res)
        self.assertTrue(os.path.exists(to_create))

        with io.open(to_create, "r", encoding="utf-8") as fp:
            actual_contents = json.load(fp)

        self.assertDictEqual(actual_contents, test_data)

        to_create = os.path.join(temp_dir.name, "my_super_chill_file.json")

        # And now when the file appears to exist
        with mock.patch("syspurpose.utils.io.open") as mock_open:
            error_to_raise = OSError()
            error_to_raise.errno = errno.EEXIST
            mock_open.side_effect = error_to_raise

            res = utils.create_file(to_create, test_data)
            self.assertFalse(res)

        to_create = os.path.join(temp_dir.name, "my_other_cool_file.json")

        # And now with an unexpected OSError
        with mock.patch("syspurpose.utils.io.open") as mock_open:
            error_to_raise = OSError()
            error_to_raise.errno = errno.E2BIG  # Anything aside from the ones expected
            mock_open.side_effect = error_to_raise

            self.assertRaises(OSError, utils.create_file, to_create, test_data)
            self.assertFalse(os.path.exists(to_create))