From 4bd1e757a0ce3ef0a7cf3b841563003404a5ce01 Mon Sep 17 00:00:00 2001 From: Chenxiong Qi Date: Jun 28 2018 07:30:58 +0000 Subject: Avoid to upload a file with different checksum The problem is, upload is able to upload a file, which is already in sources with different checksum. This fix prevents from uploading that file. Fixes #204 Signed-off-by: Chenxiong Qi --- diff --git a/pyrpkg/sources.py b/pyrpkg/sources.py index a443f38..249c4ee 100644 --- a/pyrpkg/sources.py +++ b/pyrpkg/sources.py @@ -19,7 +19,7 @@ entries, and write these entries to the file in the proper format. import os import re -from .errors import HashtypeMixingError, MalformedLineError +from .errors import rpkgError, HashtypeMixingError, MalformedLineError LINE_PATTERN = re.compile( @@ -80,6 +80,14 @@ class SourcesFile(object): if entry == e: return + if e.file == entry.file and e.hash != entry.hash: + raise rpkgError( + 'Uploading file {0} which has different checksum.\n' + 'If this is the only file in sources, please use ' + 'new-sources.\n' + 'If mutilple files are in sources, please remove this ' + 'file from sources manually.'.format(e.file)) + self.entries.append(entry) def write(self): diff --git a/tests/test_cli.py b/tests/test_cli.py index c50fdd4..d9c604d 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -1074,6 +1074,7 @@ class LookasideCacheMock(object): class TestUpload(LookasideCacheMock, CliTestCase): + """Test command upload""" def setUp(self): super(TestUpload, self).setUp() @@ -1129,6 +1130,25 @@ class TestUpload(LookasideCacheMock, CliTestCase): self.assertEqual(expected_sources_content, self.read_file(self.sources_file).strip().split('\n')) + def test_upload_file_twice_but_checksum_is_different(self): + self.write_file(os.path.join(self.cloned_repo_path, 'sources'), + content='123456 README.rst') + + readme_rst = os.path.join(self.cloned_repo_path, 'README.rst') + self.write_file(readme_rst, content='Hello rpkg') + + cli_cmd = [ + 'rpkg', '--path', self.cloned_repo_path, 'upload', readme_rst + ] + with patch('sys.argv', new=cli_cmd): + cli = self.new_cli() + with patch('pyrpkg.lookaside.CGILookasideCache.upload', + new=self.lookasidecache_upload): + six.assertRaisesRegex( + self, rpkgError, + r'.+README.rst which has different checksum.+', + cli.upload) + class TestSources(LookasideCacheMock, CliTestCase):