def test_digest_creation_bsd(self):
        """Verify the correct creation of a BSD-style file."""
        from pygeoprocessing.testing import checksum_folder

        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        DigestEquality.create_sample_folder(sample_folder)

        checksum_file = os.path.join(self.workspace, 'checksum.md5')
        checksum_folder(sample_folder, checksum_file, style='BSD')

        expected_checksum_lines = [
            '# orig_workspace = /tmp/tmpcQNvKj/tmpW_AShy', '# OS = Linux',
            '# plat_string = Linux-3.2.0-4-amd64-x86_64-with-debian-jessie',
            '# GDAL = 1.10.1', '# numpy = 1.10.4',
            '# pygeoprocessing = 0.3.0a15.post24+n314e47b1d4e9',
            '# checksum_style = BSD',
            'MD5 (_a) = 5c855e094bdf284e55e9d16627ddd64b',
            'MD5 (_b) = c716fb29298ad96a3b31757ec9755763',
            'MD5 (_c/_d) = 6bc947566bb3f50d712efb0de07bfb19'
        ]

        checksum_file_lines = open(checksum_file).read().split('\n')

        for expected_line, found_line in zip(expected_checksum_lines,
                                             checksum_file_lines):
            if expected_line.startswith('# ') and found_line.startswith('# '):
                continue
            self.assertEqual(expected_line, found_line)
    def test_gnu_checksum_file(self):
        """Verify a GNU-style checksum file."""
        from pygeoprocessing.testing import assert_checksums_equal
        from pygeoprocessing.testing import checksum_folder

        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        DigestEquality.create_sample_folder(sample_folder)

        checksum_file = os.path.join(self.workspace, 'checksum.md5')
        checksum_folder(sample_folder, checksum_file, style='GNU')

        assert_checksums_equal(checksum_file, base_folder=sample_folder)
    def test_checksum_all_files_missing(self):
        """Verify testing checksum from wrong directory fails."""
        from pygeoprocessing.testing import assert_checksums_equal
        from pygeoprocessing.testing import checksum_folder

        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        DigestEquality.create_sample_folder(sample_folder)

        checksum_file = os.path.join(self.workspace, 'checksum.md5')
        checksum_folder(sample_folder, checksum_file)

        with self.assertRaises(AssertionError):
            assert_checksums_equal(checksum_file, self.workspace)
    def test_checksum_a_modified_file(self):
        """Test for when a checksummed file has been modified."""
        from pygeoprocessing.testing import assert_checksums_equal
        from pygeoprocessing.testing import checksum_folder

        # Create the sample files, checksum the folder, then remove one of the
        # files.
        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        files = DigestEquality.create_sample_folder(sample_folder)
        checksum_file = os.path.join(self.workspace, 'checksum.md5')
        checksum_folder(sample_folder, checksum_file, style='GNU')
        open(files[0], 'a').write('foo!')

        with self.assertRaises(AssertionError):
            assert_checksums_equal(checksum_file, sample_folder)
    def test_checksum_assertion_in_cwd(self):
        """Verify testing from CWD if none is provided."""
        from pygeoprocessing.testing import assert_checksums_equal
        from pygeoprocessing.testing import checksum_folder

        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        DigestEquality.create_sample_folder(sample_folder)

        checksum_file = os.path.join(self.workspace, 'checksum.md5')
        checksum_folder(sample_folder, checksum_file)

        try:
            cwd = os.getcwd()
            os.chdir(sample_folder)
            assert_checksums_equal(checksum_file)
        finally:
            os.chdir(cwd)
    def test_windows_pathsep_replacement(self):
        """Verify that windows-style path separation works as expected."""
        from pygeoprocessing.testing import checksum_folder

        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        DigestEquality.create_sample_folder(sample_folder)
        checksum_file = os.path.join(self.workspace, 'checksum.md5')

        # Simulate being on Windows.
        # On *NIX systems, this shouldn't affect the output files at all, since
        # we're replacing os.sep ('/' on *NIX) with '/'.
        with mock.patch('platform.system', lambda: 'Windows'):
            # Just to verify that sys.platform() is currently set to Windows.
            self.assertEqual(platform.system(), 'Windows')
            checksum_folder(sample_folder, checksum_file, style='GNU')
            last_line = open(checksum_file).read().split('\n')[-2]
            self.assertEqual(last_line,
                             '6bc947566bb3f50d712efb0de07bfb19  _c/_d')
    def test_digest_creation_skip_extensions(self):
        """Test the ignore_exts parameter for checksum file creation."""
        from pygeoprocessing.testing import checksum_folder

        sample_folder = tempfile.mkdtemp(dir=self.workspace)
        DigestEquality.create_sample_folder(sample_folder)

        # create a new file with the .ignore extension within the sample
        # folder.  This file should not appear in the checksum file at all.
        open(os.path.join(sample_folder, 'foo.ignore'), 'w').write('foo')

        checksum_file = os.path.join(self.workspace, 'checksum.md5')
        checksum_folder(sample_folder,
                        checksum_file,
                        style='GNU',
                        ignore_exts=['.ignore'])

        self.assertTrue('.ignore' not in open(checksum_file).read())
    def test_digest_creation_bad_type(self):
        """Verify an exception is raised when a bad digest type is given."""
        from pygeoprocessing.testing import checksum_folder

        with self.assertRaises(IOError):
            checksum_folder("doesn't matter", "doesn't matter", 'bad type!')