MORSECorp / snappiershot

Apache License 2.0
30 stars 2 forks source link

[BUG] JSON IO Write Permission Error #89

Open sco1 opened 3 years ago

sco1 commented 3 years ago

Describe the bug Exposed by a failure of the SnapshotTracker testing, the io.write_json_file function will raise a FileExistsError when attempting to rename the temporary file if a snapshot JSON is already present.

To Reproduce Run the test suite on Windows (after accounting for #87)

self = WindowsPath('C:/Users/sco1/AppData/Local/Temp/pytest-of-sco1/pytest-0/test_tracker0/.snapshots/test_file_2.temp')
target = WindowsPath('C:/Users/sco1/AppData/Local/Temp/pytest-of-sco1/pytest-0/test_tracker0/.snapshots/test_file_2.json')

    def rename(self, target):
        """
        Rename this path to the target path.

        The target path may be absolute or relative. Relative paths are
        interpreted relative to the current working directory, *not* the
        directory of the Path object.

        Returns the new Path instance pointing to the target path.
        """
>       self._accessor.rename(self, target)
E       FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:\\Users\\sco1\\AppData\\Local\\Temp\\pytest-of-sco1\\pytest-0\\test_tracker0\\.snapshots\\test_file_2.temp' -> 'C:\\Users\\sco1\\AppData\\Local\\Temp\\pytest-of-sco1\\pytest-0\\test_tracker0\\.snapshots\\test_file_2.json'

Expected behavior No Errors

Versions:

Additional context The Path.rename call here:

https://github.com/MORSECorp/snappiershot/blob/acb6a8d01d4496abe0f2fe83c7e7af9cf77aac8e/snappiershot/serializers/io.py#L47-L51

Will only silently replace on Unix, on Windows it will (may?) raise a FileExistsError. Changing to temporary_file.replace(file) appears to resolve the problem.