| |
@@ -1,9 +1,18 @@
|
| |
+ import os
|
| |
+ import tempfile
|
| |
import unittest
|
| |
import warnings
|
| |
|
| |
import mock
|
| |
|
| |
- from pyrpkg.utils import cached_property, log_result, warn_deprecated
|
| |
+ from pyrpkg.utils import (
|
| |
+ cached_property,
|
| |
+ is_file_in_directory,
|
| |
+ is_file_tracked,
|
| |
+ log_result,
|
| |
+ warn_deprecated,
|
| |
+ )
|
| |
+ from utils import CommandTestCase
|
| |
|
| |
|
| |
class CachedPropertyTestCase(unittest.TestCase):
|
| |
@@ -196,3 +205,86 @@
|
| |
]
|
| |
log_result(self.log_func, obj)
|
| |
self.assertEqual(self.logs, expected)
|
| |
+
|
| |
+
|
| |
+ class FileInDirectoryTestCase(unittest.TestCase):
|
| |
+ def test_is_file_in_directory(self):
|
| |
+ expected = (
|
| |
+ # file, directory, expected result
|
| |
+ ("/repo/fedpkg/sources", "/repo/fedpkg", "sources"),
|
| |
+ ("/repo/fedpkg/subdir/sources", "/repo/fedpkg", "subdir/sources"),
|
| |
+ ("/repo/fedpkg/sources/", "/repo/fedpkg", "sources"),
|
| |
+ ("/repo/fedpkg/sources", "/repo/fedpkg/", "sources"),
|
| |
+ ("/repo/file", "/", "repo/file"),
|
| |
+ ("/file", "/", "file"),
|
| |
+ ("sources", ".", "sources"),
|
| |
+ ("data/file", ".", "data/file"),
|
| |
+ ("dir/myrepo/sources", "dir/myrepo", "sources"),
|
| |
+
|
| |
+ ("/sources", "/repo/fedpkg", None),
|
| |
+ ("/", "bad_dir_name", None),
|
| |
+ ("./", "./a", None),
|
| |
+ ("./b", "./a/", None),
|
| |
+ ("sources", "./dir", None),
|
| |
+ ("f", "./dir", None),
|
| |
+ ("file", "dir/myrepo", None),
|
| |
+ ("a/bbb", "/a/b", None),
|
| |
+ ("a/b", "/a/bbb", None),
|
| |
+ )
|
| |
+ for num, case in enumerate(expected):
|
| |
+ (file_path, dir_path, expected_result) = case
|
| |
+ self.assertEqual(
|
| |
+ is_file_in_directory(file_path, dir_path),
|
| |
+ expected_result,
|
| |
+ "The case num {0} has failed".format(num),
|
| |
+ )
|
| |
+
|
| |
+
|
| |
+ class FileTrackedTestCase(CommandTestCase):
|
| |
+ def test_file_outside_of_repo(self):
|
| |
+ # check file outside of the test repo
|
| |
+ self.assertFalse(
|
| |
+ is_file_tracked("external_filename", self.repo_path)
|
| |
+ )
|
| |
+
|
| |
+ def test_actual_file_in_repo(self):
|
| |
+ # check actual file from the test repo
|
| |
+ self.assertTrue(
|
| |
+ is_file_tracked(
|
| |
+ os.path.join(self.repo_path, "sources"),
|
| |
+ self.repo_path
|
| |
+ )
|
| |
+ )
|
| |
+
|
| |
+ def test_newly_created_file_in_repo(self):
|
| |
+ # check newly created file in the test repo
|
| |
+ temp_file = tempfile.NamedTemporaryFile(dir=self.repo_path)
|
| |
+ self.assertFalse(
|
| |
+ is_file_tracked(temp_file.name, self.repo_path)
|
| |
+ )
|
| |
+ temp_file.close()
|
| |
+
|
| |
+ def test_newly_added_file_to_stage(self):
|
| |
+ # check newly added file to stage in the test repo
|
| |
+ temp_file = tempfile.NamedTemporaryFile(dir=self.repo_path)
|
| |
+ temp_file_basename = os.path.basename(temp_file.name)
|
| |
+ self.run_cmd(["git", "add", temp_file_basename], cwd=self.repo_path)
|
| |
+ self.assertTrue(
|
| |
+ is_file_tracked(temp_file.name, self.repo_path)
|
| |
+ )
|
| |
+ temp_file.close()
|
| |
+
|
| |
+ def test_not_existing_file(self):
|
| |
+ # check not existing file
|
| |
+ self.assertFalse(
|
| |
+ is_file_tracked("/file_doesnt_exist", self.repo_path)
|
| |
+ )
|
| |
+
|
| |
+ def test_not_existing_file_should_belong_to_repo(self):
|
| |
+ # check not existing file that should belong to the test repo
|
| |
+ self.assertFalse(
|
| |
+ is_file_tracked(
|
| |
+ os.path.join(self.repo_path, "file_doesnt_exist"),
|
| |
+ self.repo_path
|
| |
+ )
|
| |
+ )
|
| |
Implements additional protection (except .gitignore warning) that
prevents overwrite staged files when x-pkg sources is run. And forbids
uploading staged files that may not belong to lookaside cache.
Fixes: https://pagure.io/fedpkg/issue/241
JIRA: COMPOSE-2689
Signed-off-by: Ondrej Nosek onosek@redhat.com