| 64 | @pytest.mark.skip_if_binaries_missing("git") |
| 65 | class GitModuleTest(ModuleCase): |
| 66 | def setUp(self): |
| 67 | super().setUp() |
| 68 | self.repo = tempfile.mkdtemp(dir=RUNTIME_VARS.TMP) |
| 69 | self.addCleanup(shutil.rmtree, self.repo, ignore_errors=True) |
| 70 | self.addCleanup(delattr, self, "repo") |
| 71 | self.files = ("foo", "bar", "baz", "питон") |
| 72 | self.addCleanup(delattr, self, "files") |
| 73 | self.dirs = ("", "qux") |
| 74 | self.addCleanup(delattr, self, "dirs") |
| 75 | self.branches = ("master", "iamanewbranch") |
| 76 | self.addCleanup(delattr, self, "branches") |
| 77 | self.tags = ("git_testing",) |
| 78 | self.addCleanup(delattr, self, "tags") |
| 79 | for dirname in self.dirs: |
| 80 | dir_path = pathlib.Path(self.repo) / dirname |
| 81 | dir_path.mkdir(parents=True, exist_ok=True) |
| 82 | for filename in self.files: |
| 83 | with salt.utils.files.fopen(str(dir_path / filename), "wb") as fp_: |
| 84 | fp_.write(f"This is a test file named {filename}.".encode()) |
| 85 | # Navigate to the root of the repo to init, stage, and commit |
| 86 | with pytest.helpers.change_cwd(self.repo): |
| 87 | # Initialize a new git repository |
| 88 | subprocess.check_call(["git", "init", "--quiet", self.repo]) |
| 89 | |
| 90 | # Set user.name and user.email config attributes if not present |
| 91 | for key, value in ( |
| 92 | ("user.name", "Jenkins"), |
| 93 | ("user.email", "qa@saltstack.com"), |
| 94 | ): |
| 95 | # Check if key is missing |
| 96 | keycheck = subprocess.Popen( |
| 97 | ["git", "config", "--get", "--global", key], |
| 98 | stdout=subprocess.PIPE, |
| 99 | stderr=subprocess.PIPE, |
| 100 | ) |
| 101 | if keycheck.wait() != 0: |
| 102 | # Set the key if it is not present |
| 103 | subprocess.check_call(["git", "config", "--global", key, value]) |
| 104 | |
| 105 | subprocess.check_call(["git", "add", "."]) |
| 106 | subprocess.check_call( |
| 107 | ["git", "commit", "--quiet", "--message", "Initial commit"] |
| 108 | ) |
| 109 | # Add a tag |
| 110 | subprocess.check_call(["git", "tag", "-a", self.tags[0], "-m", "Add tag"]) |
| 111 | # Checkout a second branch |
| 112 | subprocess.check_call( |
| 113 | ["git", "checkout", "--quiet", "-b", self.branches[1]] |
| 114 | ) |
| 115 | # Add a line to the file |
| 116 | with salt.utils.files.fopen(self.files[0], "a") as fp_: |
| 117 | fp_.write(salt.utils.stringutils.to_str("Added a line\n")) |
| 118 | # Commit the updated file |
| 119 | subprocess.check_call( |
| 120 | [ |
| 121 | "git", |
| 122 | "commit", |
| 123 | "--quiet", |