|
| 1 | +#!/usr/bin/env python3 |
| 2 | +import inspect |
| 3 | +import os |
| 4 | +import tarfile |
| 5 | +import time |
| 6 | +from pathlib import Path |
| 7 | +from unittest import mock |
| 8 | + |
| 9 | +import pytest |
| 10 | + |
| 11 | +from yo.tasks import build_tarball |
| 12 | +from yo.tasks import standardize_globs |
| 13 | +from yo.tasks import YoTask |
| 14 | +from yo.util import YoExc |
| 15 | + |
| 16 | + |
| 17 | +@pytest.fixture(autouse=True) |
| 18 | +def mock_all_tasks(): |
| 19 | + with mock.patch("yo.tasks.list_tasks") as m: |
| 20 | + m.return_value = [ |
| 21 | + "task_one", |
| 22 | + "task_two", |
| 23 | + "task_three", |
| 24 | + "task_four", |
| 25 | + "task_five", |
| 26 | + "task_six", |
| 27 | + ] |
| 28 | + yield |
| 29 | + |
| 30 | + |
| 31 | +def test_depends_conflicts(): |
| 32 | + contents = inspect.cleandoc( |
| 33 | + """ |
| 34 | + DEPENDS_ON task_one |
| 35 | + DEPENDS_ON task_two |
| 36 | + MAYBE_DEPENDS_ON task_three |
| 37 | + MAYBE_DEPENDS_ON dne_one |
| 38 | + CONFLICTS_WITH task_four |
| 39 | + CONFLICTS_WITH dne_two |
| 40 | + PREREQ_FOR task_five |
| 41 | + PREREQ_FOR dne_three |
| 42 | + """ |
| 43 | + ) |
| 44 | + |
| 45 | + task = YoTask.create_from_string("test_task", contents) |
| 46 | + |
| 47 | + # the maybe_depends_on dne_one is dropped since it doesn't exist |
| 48 | + assert task.dependencies == ["task_one", "task_two", "task_three"] |
| 49 | + # all conflicts are kept |
| 50 | + assert task.conflicts == ["task_four", "dne_two"] |
| 51 | + # all prereqs are kept |
| 52 | + assert task.prereq_for == ["task_five", "dne_three"] |
| 53 | + |
| 54 | + assert task.script == contents.replace( |
| 55 | + "MAYBE_DEPENDS_ON dne_one", |
| 56 | + "# MAYBE_DEPENDS_ON dne_one", |
| 57 | + ).replace("MAYBE_DEPENDS_ON task_three", "DEPENDS_ON task_three") |
| 58 | + |
| 59 | + |
| 60 | +def test_include_files(): |
| 61 | + contents = inspect.cleandoc( |
| 62 | + """ |
| 63 | + INCLUDE_FILE ~/.profile |
| 64 | + INCLUDE_FILE ~/Documents/"Important File.docx" /usr/share/docs/"Important File".docx |
| 65 | + INCLUDE_FILE ~/dotfiles/bashrc_oci.sh ~/.bashrc |
| 66 | + MAYBE_INCLUDE_FILE ~/.cache/yo.*.json ~/.cache/ |
| 67 | + SENDFILE ~/data.tar.gz |
| 68 | + SENDFILE ~/"Special File" |
| 69 | + """ |
| 70 | + ) |
| 71 | + |
| 72 | + task = YoTask.create_from_string("test_task", contents) |
| 73 | + assert task.include_files == [ |
| 74 | + ("~/.profile", "~/.profile", False), |
| 75 | + ( |
| 76 | + "~/Documents/Important File.docx", |
| 77 | + "/usr/share/docs/Important File.docx", |
| 78 | + False, |
| 79 | + ), |
| 80 | + ("~/dotfiles/bashrc_oci.sh", "~/.bashrc", False), |
| 81 | + ("~/.cache/yo.*.json", "~/.cache/", True), |
| 82 | + ] |
| 83 | + assert task.sendfiles == [ |
| 84 | + Path.home() / "data.tar.gz", |
| 85 | + Path.home() / "Special File", |
| 86 | + ] |
| 87 | + |
| 88 | + expected_contents = "\n".join("# " + line for line in contents.split("\n")) |
| 89 | + assert task.script == expected_contents |
| 90 | + assert task.script == expected_contents |
| 91 | + |
| 92 | + |
| 93 | +def test_wrong_args(): |
| 94 | + cases = [ |
| 95 | + "INCLUDE_FILE", |
| 96 | + "MAYBE_INCLUDE_FILE", |
| 97 | + "INCLUDE_FILE one two three", |
| 98 | + "MAYBE_INCLUDE_FILE one two three", |
| 99 | + "SENDFILE", |
| 100 | + "SENDFILE one two", |
| 101 | + ] |
| 102 | + for case in cases: |
| 103 | + with pytest.raises(YoExc): |
| 104 | + YoTask.create_from_string("test_task", case) |
| 105 | + |
| 106 | + |
| 107 | +def test_standardize_globs(): |
| 108 | + assert standardize_globs( |
| 109 | + [ |
| 110 | + # The standard: copy to the same path |
| 111 | + ("~/.bashrc", "~/.bashrc", False), |
| 112 | + # An unusual: copy from homedir to a system path |
| 113 | + ("~/.bashrc", "/etc/bashrc", False), |
| 114 | + # Also unusual: copy from system path to homedir |
| 115 | + ("/etc/bashrc", "~/.bashrc", True), |
| 116 | + ] |
| 117 | + ) == ( |
| 118 | + [ |
| 119 | + (str(Path.home() / ".bashrc"), ".bashrc", False), |
| 120 | + ("/etc/bashrc", ".bashrc", True), |
| 121 | + ], |
| 122 | + [ |
| 123 | + (str(Path.home() / ".bashrc"), "etc/bashrc", False), |
| 124 | + ], |
| 125 | + ) |
| 126 | + |
| 127 | + # neither side can be relative |
| 128 | + failing = [ |
| 129 | + [("relative path", "~/.bashrc", False)], |
| 130 | + [("/foobar", ".bashrc", False)], |
| 131 | + ] |
| 132 | + for case in failing: |
| 133 | + with pytest.raises(YoExc): |
| 134 | + standardize_globs(case) |
| 135 | + |
| 136 | + |
| 137 | +def create_test_dir(tmp_path): |
| 138 | + # The important cases to cover are: |
| 139 | + # 1. A regular file |
| 140 | + # 2. A directory being included recursively |
| 141 | + # 3. A glob matching several files or directories |
| 142 | + tarball = tmp_path / "tarball.tar.gz" |
| 143 | + |
| 144 | + bashrc = tmp_path / ".bashrc" |
| 145 | + bashrc.write_text("export PS2='$ '") |
| 146 | + |
| 147 | + bash_history = tmp_path / ".bash_history" |
| 148 | + bash_history.write_text(":(){ :|:& };:") |
| 149 | + |
| 150 | + note_dir = tmp_path / "my-notes" |
| 151 | + note_dir.mkdir() |
| 152 | + note_one = note_dir / "one.txt" |
| 153 | + note_one.write_text("some data") |
| 154 | + note_two = note_dir / "two.txt" |
| 155 | + note_two.write_text("some other data") |
| 156 | + unmatched_note = note_dir / "not included.md" |
| 157 | + unmatched_note.write_text("I won't be in the tarball") |
| 158 | + |
| 159 | + doc_dir = tmp_path / "my-docs" |
| 160 | + doc_dir.mkdir() |
| 161 | + doc_one = doc_dir / ".hidden_document" |
| 162 | + doc_one.write_text("test data") |
| 163 | + doc_two = doc_dir / "an important, space-filled document title" |
| 164 | + doc_two.write_text("more test data!") |
| 165 | + doc_subdir = doc_dir / "Project" |
| 166 | + doc_subdir.mkdir() |
| 167 | + project_doc = doc_subdir / "Rollout plan.md" |
| 168 | + project_doc.write_text( |
| 169 | + "steps 1: write a plan, step 2: ???, step 3: profit!" |
| 170 | + ) |
| 171 | + |
| 172 | + included = [ |
| 173 | + bashrc, |
| 174 | + note_one, |
| 175 | + note_two, |
| 176 | + doc_dir, |
| 177 | + doc_one, |
| 178 | + doc_two, |
| 179 | + doc_subdir, |
| 180 | + project_doc, |
| 181 | + ] |
| 182 | + excluded = [unmatched_note, bash_history] |
| 183 | + return tarball, included, excluded |
| 184 | + |
| 185 | + |
| 186 | +def test_build_tarball_skip(tmp_path): |
| 187 | + ctx = mock.Mock() |
| 188 | + name = "test_task" |
| 189 | + |
| 190 | + tarball, included, excluded = create_test_dir(tmp_path) |
| 191 | + base = str(tmp_path) |
| 192 | + include_files = [ |
| 193 | + (f"{base}/.bashrc", ".bashrc", False), |
| 194 | + (f"{base}/my-notes/*.txt", "notes/", False), |
| 195 | + (f"{base}/my-docs/", "docs/", False), |
| 196 | + ] |
| 197 | + |
| 198 | + TEST_TIME = int(time.time()) |
| 199 | + |
| 200 | + # Set the mtimes to be older than the tarball |
| 201 | + for path in included: |
| 202 | + os.utime(path, times=(TEST_TIME, TEST_TIME - 5)) |
| 203 | + # Set the excluded paths to be newer than the tarball, |
| 204 | + # demonstrating that they don't impact the generation. |
| 205 | + for path in excluded: |
| 206 | + os.utime(path, times=(TEST_TIME, TEST_TIME + 5)) |
| 207 | + |
| 208 | + tarball.write_text("foobar") |
| 209 | + os.utime(tarball, times=(TEST_TIME, TEST_TIME)) |
| 210 | + |
| 211 | + with mock.patch("yo.tasks.subprocess") as m: |
| 212 | + build_tarball(ctx, include_files, tmp_path, tarball, name) |
| 213 | + # Subprocess should not have been called at all |
| 214 | + assert not m.mock_calls |
| 215 | + # Ctx should not have been called at all |
| 216 | + assert not ctx.mock_calls |
| 217 | + |
| 218 | + # ensure that making any file newer results in a rebuilt tarball |
| 219 | + for path in included: |
| 220 | + os.utime(path, times=(TEST_TIME, TEST_TIME + 5)) |
| 221 | + with mock.patch("yo.tasks.subprocess") as m: |
| 222 | + build_tarball(ctx, include_files, tmp_path, tarball, name) |
| 223 | + assert m.mock_calls |
| 224 | + assert ctx.mock_calls |
| 225 | + ctx.reset_mock() |
| 226 | + os.utime(path, times=(TEST_TIME, TEST_TIME - 5)) |
| 227 | + |
| 228 | + |
| 229 | +def test_build_tarball(tmp_path): |
| 230 | + ctx = mock.Mock() |
| 231 | + name = "test_task" |
| 232 | + |
| 233 | + tarball, included, excluded = create_test_dir(tmp_path) |
| 234 | + base = str(tmp_path) |
| 235 | + include_files = [ |
| 236 | + (f"{base}/.bashrc", ".bashrc", False), |
| 237 | + (f"{base}/my-notes/*.txt", "notes/", False), |
| 238 | + (f"{base}/my-docs/", "docs/", False), |
| 239 | + ] |
| 240 | + |
| 241 | + with mock.patch("yo.tasks.subprocess") as m: |
| 242 | + build_tarball(ctx, include_files, tmp_path, tarball, name) |
| 243 | + assert len(m.run.mock_calls) == 1 |
| 244 | + assert m.run.mock_calls[0].args[0][:3] == ["tar", "-czhf", tarball] |
| 245 | + assert sorted(m.run.mock_calls[0].args[0][3:]) == sorted( |
| 246 | + [".bashrc", "notes/one.txt", "notes/two.txt", "docs"] |
| 247 | + ) |
| 248 | + ctx.con.log.assert_called() |
| 249 | + |
| 250 | + |
| 251 | +def test_real_tarball(tmp_path): |
| 252 | + ctx = mock.Mock() |
| 253 | + name = "test_task" |
| 254 | + |
| 255 | + tarball, included, excluded = create_test_dir(tmp_path) |
| 256 | + base = str(tmp_path) |
| 257 | + include_files = [ |
| 258 | + (f"{base}/.bashrc", ".bashrc", False), |
| 259 | + (f"{base}/my-notes/*.txt", "notes/", False), |
| 260 | + (f"{base}/my-docs/", "docs/", False), |
| 261 | + ] |
| 262 | + |
| 263 | + build_tarball(ctx, include_files, tmp_path, tarball, name) |
| 264 | + ctx.con.log.assert_called() |
| 265 | + |
| 266 | + tf = tarfile.open(tarball) |
| 267 | + expected_members = [ |
| 268 | + ".bashrc", |
| 269 | + "notes/one.txt", |
| 270 | + "notes/two.txt", |
| 271 | + "docs", |
| 272 | + "docs/.hidden_document", |
| 273 | + "docs/an important, space-filled document title", |
| 274 | + "docs/Project", |
| 275 | + "docs/Project/Rollout plan.md", |
| 276 | + ] |
| 277 | + assert sorted(tf.getnames()) == sorted(expected_members) |
0 commit comments