-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_install.py
More file actions
622 lines (492 loc) · 20.4 KB
/
test_install.py
File metadata and controls
622 lines (492 loc) · 20.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
#!/usr/bin/env python3
"""
Unit tests for install.py
This module contains comprehensive tests for the install script functions,
including argument parsing, file downloading, and file editing functionality.
"""
import argparse
import pathlib
import shutil
import tempfile
import unittest
from unittest.mock import patch
# Import the functions we want to test
from install import (
download_file,
edit_source_directory,
install,
make_parser,
update_reusable_workflow_references,
)
class TestMakeParser(unittest.TestCase):
"""Test the command-line argument parser."""
def setUp(self):
self.parser = make_parser()
def test_parser_creation(self):
"""Test that parser is created successfully."""
self.assertIsInstance(self.parser, argparse.ArgumentParser)
def test_platform_choices(self):
"""Test that platform argument accepts valid choices."""
# Test valid platforms
for platform in ["astro", "hugo", "jekyll"]:
args = self.parser.parse_args([platform])
self.assertEqual(args.platform, platform)
def test_invalid_platform(self):
"""Test that invalid platform raises SystemExit."""
with self.assertRaises(SystemExit):
self.parser.parse_args(["invalid"])
def test_default_values(self):
"""Test default values for optional arguments."""
args = self.parser.parse_args(["hugo"])
self.assertIsNone(args.branch)
self.assertIsNone(args.tag)
self.assertIsNone(args.commit)
self.assertEqual(args.site_dir, ".")
self.assertEqual(args.output_dir, ".github/workflows")
self.assertEqual(args.repo, "omsf/static-site-tools")
def test_custom_values(self):
"""Test custom values for optional arguments."""
args = self.parser.parse_args(
[
"astro",
"--tag",
"v1.0.0",
"--site-dir",
"my-site",
"--output-dir",
"custom/workflows",
"--repo",
"myorg/my-repo",
]
)
self.assertEqual(args.platform, "astro")
self.assertEqual(args.tag, "v1.0.0")
self.assertIsNone(args.branch)
self.assertIsNone(args.commit)
self.assertEqual(args.site_dir, "my-site")
self.assertEqual(args.output_dir, "custom/workflows")
self.assertEqual(args.repo, "myorg/my-repo")
def test_tag_argument(self):
"""Test tag argument works."""
args = self.parser.parse_args(["hugo", "--tag", "v2.0.0"])
self.assertEqual(args.tag, "v2.0.0")
self.assertIsNone(args.branch)
self.assertIsNone(args.commit)
def test_commit_argument(self):
"""Test commit argument works."""
args = self.parser.parse_args(["astro", "--commit", "abc123"])
self.assertEqual(args.commit, "abc123")
self.assertIsNone(args.branch)
self.assertIsNone(args.tag)
def test_branch_argument(self):
"""Test branch argument works."""
args = self.parser.parse_args(["jekyll", "--branch", "develop"])
self.assertEqual(args.branch, "develop")
self.assertIsNone(args.tag)
self.assertIsNone(args.commit)
def test_mutually_exclusive_arguments(self):
"""Test that version arguments are mutually exclusive."""
with self.assertRaises(SystemExit):
self.parser.parse_args(["hugo", "--tag", "v1.0.0", "--branch", "main"])
with self.assertRaises(SystemExit):
self.parser.parse_args(["astro", "--commit", "abc123", "--tag", "v1.0.0"])
with self.assertRaises(SystemExit):
self.parser.parse_args(
["jekyll", "--branch", "develop", "--commit", "abc123"]
)
def test_custom_repo(self):
"""Test custom repository argument."""
args = self.parser.parse_args(["jekyll", "--repo", "custom/repository"])
self.assertEqual(args.repo, "custom/repository")
class TestDownloadFile(unittest.TestCase):
"""Test the file download functionality."""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)
@patch("install.urllib.request.urlretrieve")
@patch("install.print")
def test_download_file_success(self, mock_print, mock_urlretrieve):
"""Test successful file download."""
url = "https://example.com/file.yaml"
destination = pathlib.Path(self.temp_dir) / "test.yaml"
download_file(url, destination)
# Check that urlretrieve was called with correct arguments
mock_urlretrieve.assert_called_once_with(url, destination)
# Check that appropriate messages were printed
mock_print.assert_any_call(f"Downloading {url} to {destination}...")
mock_print.assert_any_call("Download complete.")
@patch("install.urllib.request.urlretrieve")
def test_download_file_creates_directory(self, mock_urlretrieve):
"""Test that download_file creates parent directories."""
url = "https://example.com/file.yaml"
nested_path = pathlib.Path(self.temp_dir) / "nested" / "dir" / "test.yaml"
download_file(url, nested_path)
# Check that parent directory was created
self.assertTrue(nested_path.parent.exists())
mock_urlretrieve.assert_called_once_with(url, nested_path)
class TestEditSourceDirectory(unittest.TestCase):
"""Test the file editing functionality."""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)
def test_edit_source_directory_hugo(self):
"""Test editing Hugo workflow file."""
content = """name: Build Hugo site from PR
env:
HUGO_SOURCE_DIR: "example-hugo"
HUGO_BASE_URL: ""
HUGO_OUTPUT_DIR: "public"
"""
expected = """name: Build Hugo site from PR
env:
HUGO_SOURCE_DIR: "my-hugo-site"
HUGO_BASE_URL: ""
HUGO_OUTPUT_DIR: "public"
"""
file_path = pathlib.Path(self.temp_dir) / "test.yaml"
with open(file_path, "w") as f:
f.write(content)
edit_source_directory(file_path, "hugo", "my-hugo-site")
with open(file_path, "r") as f:
result = f.read()
self.assertEqual(result, expected)
def test_edit_source_directory_astro(self):
"""Test editing Astro workflow file."""
content = """name: Build Astro site from PR
env:
ASTRO_SOURCE_DIR: "example-astro"
ASTRO_BASE_URL: ""
"""
expected = """name: Build Astro site from PR
env:
ASTRO_SOURCE_DIR: "my-astro-app"
ASTRO_BASE_URL: ""
"""
file_path = pathlib.Path(self.temp_dir) / "test.yaml"
with open(file_path, "w") as f:
f.write(content)
edit_source_directory(file_path, "astro", "my-astro-app")
with open(file_path, "r") as f:
result = f.read()
self.assertEqual(result, expected)
def test_edit_source_directory_jekyll(self):
"""Test editing Jekyll workflow file."""
content = """name: Build Jekyll site from PR
env:
JEKYLL_SOURCE_DIR: "example-jekyll"
JEKYLL_BASE_URL: ""
"""
expected = """name: Build Jekyll site from PR
env:
JEKYLL_SOURCE_DIR: "docs"
JEKYLL_BASE_URL: ""
"""
file_path = pathlib.Path(self.temp_dir) / "test.yaml"
with open(file_path, "w") as f:
f.write(content)
edit_source_directory(file_path, "jekyll", "docs")
with open(file_path, "r") as f:
result = f.read()
self.assertEqual(result, expected)
def test_edit_source_directory_no_match(self):
"""Test editing file with no matching pattern."""
content = """name: Some other workflow
env:
OTHER_DIR: "something"
"""
file_path = pathlib.Path(self.temp_dir) / "test.yaml"
with open(file_path, "w") as f:
f.write(content)
original_content = content
edit_source_directory(file_path, "hugo", "my-site")
with open(file_path, "r") as f:
result = f.read()
# Content should remain unchanged
self.assertEqual(result, original_content)
def test_edit_source_directory_multiple_occurrences(self):
"""Test editing file with multiple occurrences of the pattern."""
content = """name: Test workflow
env:
HUGO_SOURCE_DIR: "example-hugo"
jobs:
test:
steps:
- name: Test with "example-hugo" directory
run: echo "example-hugo"
"""
expected = """name: Test workflow
env:
HUGO_SOURCE_DIR: "my-site"
jobs:
test:
steps:
- name: Test with "my-site" directory
run: echo "my-site"
"""
file_path = pathlib.Path(self.temp_dir) / "test.yaml"
with open(file_path, "w") as f:
f.write(content)
edit_source_directory(file_path, "hugo", "my-site")
with open(file_path, "r") as f:
result = f.read()
self.assertEqual(result, expected)
class TestUpdateReusableWorkflowReferences(unittest.TestCase):
"""Test pinning reusable workflow references."""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)
self.file_path = pathlib.Path(self.temp_dir) / "workflow.yaml"
def test_updates_default_repo_references(self):
content = """jobs:
build:
steps:
- uses: omsf/static-site-tools/build/hugo@main
- uses: actions/checkout@v4
"""
expected = """jobs:
build:
steps:
- uses: omsf/static-site-tools/build/hugo@v1.2.3
- uses: actions/checkout@v4
"""
self.file_path.write_text(content)
update_reusable_workflow_references(
self.file_path, "omsf/static-site-tools", "v1.2.3"
)
self.assertEqual(self.file_path.read_text(), expected)
def test_updates_multiple_repo_variants(self):
content = """jobs:
deploy:
steps:
- uses: omsf/static-site-tools/build/hugo@main
- uses: myfork/static-site-tools/common-tests@main
"""
expected = """jobs:
deploy:
steps:
- uses: omsf/static-site-tools/build/hugo@abc123
- uses: myfork/static-site-tools/common-tests@abc123
"""
self.file_path.write_text(content)
update_reusable_workflow_references(
self.file_path, "myfork/static-site-tools", "abc123"
)
self.assertEqual(self.file_path.read_text(), expected)
def test_no_update_when_no_matching_repo(self):
content = """jobs:
lint:
steps:
- uses: actions/checkout@v4
"""
self.file_path.write_text(content)
update_reusable_workflow_references(self.file_path, "custom/repo", "main")
self.assertEqual(self.file_path.read_text(), content)
class TestInstall(unittest.TestCase):
"""Test the main install function."""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_default_parameters(self, mock_download, mock_edit, mock_update):
"""Test install function with default parameters."""
install("hugo", "branch", "main")
# Check that download_file was called for each expected file
expected_files = [
"build-pr.yaml",
"build-push.yaml",
"cleanup-cloudflare.yaml",
"prod-cloudflare.yaml",
"stage-cloudflare.yaml",
]
self.assertEqual(mock_download.call_count, len(expected_files))
self.assertEqual(mock_edit.call_count, len(expected_files))
self.assertEqual(mock_update.call_count, len(expected_files))
# Check URLs and destinations
for i, file in enumerate(expected_files):
expected_url = (
"https://raw.githubusercontent.com/omsf/static-site-tools/"
"refs/heads/main/.github/workflows/example-hugo-"
f"{file}"
)
expected_dest = pathlib.Path(".github/workflows") / file
args, _ = mock_download.call_args_list[i]
self.assertEqual(args[0], expected_url)
self.assertEqual(args[1], expected_dest)
update_args, _ = mock_update.call_args_list[i]
self.assertEqual(update_args[0], expected_dest)
self.assertEqual(update_args[1], "omsf/static-site-tools")
self.assertEqual(update_args[2], "main")
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_custom_parameters(self, mock_download, mock_edit, mock_update):
"""Test install function with custom parameters."""
install("astro", "tag", "v1.0.0", "custom/workflows", "my-astro-site")
expected_files = [
"build-pr.yaml",
"build-push.yaml",
"cleanup-cloudflare.yaml",
"prod-cloudflare.yaml",
"stage-cloudflare.yaml",
]
# Check that files are downloaded to custom directory
for i, file in enumerate(expected_files):
expected_url = (
"https://raw.githubusercontent.com/omsf/static-site-tools/"
"refs/tags/v1.0.0/.github/workflows/example-astro-"
f"{file}"
)
expected_dest = pathlib.Path("custom/workflows") / file
download_args, _ = mock_download.call_args_list[i]
self.assertEqual(download_args[0], expected_url)
self.assertEqual(download_args[1], expected_dest)
edit_args, _ = mock_edit.call_args_list[i]
self.assertEqual(edit_args[0], expected_dest)
self.assertEqual(edit_args[1], "astro")
self.assertEqual(edit_args[2], "my-astro-site")
update_args, _ = mock_update.call_args_list[i]
self.assertEqual(update_args[0], expected_dest)
self.assertEqual(update_args[1], "omsf/static-site-tools")
self.assertEqual(update_args[2], "v1.0.0")
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_custom_repo(self, mock_download, mock_edit, mock_update):
"""Test install function with custom repository."""
install(
"hugo", "branch", "main", ".github/workflows", ".", "myorg/my-static-tools"
)
expected_files = [
"build-pr.yaml",
"build-push.yaml",
"cleanup-cloudflare.yaml",
"prod-cloudflare.yaml",
"stage-cloudflare.yaml",
]
# Check that files are downloaded from custom repository
for i, file in enumerate(expected_files):
expected_url = (
"https://raw.githubusercontent.com/myorg/my-static-tools/"
"refs/heads/main/.github/workflows/example-hugo-"
f"{file}"
)
expected_dest = pathlib.Path(".github/workflows") / file
download_args, _ = mock_download.call_args_list[i]
self.assertEqual(download_args[0], expected_url)
self.assertEqual(download_args[1], expected_dest)
update_args, _ = mock_update.call_args_list[i]
self.assertEqual(update_args[0], expected_dest)
self.assertEqual(update_args[1], "myorg/my-static-tools")
self.assertEqual(update_args[2], "main")
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_tag_version(self, mock_download, mock_edit, mock_update):
"""Test install function with tag version."""
install("jekyll", "tag", "v2.1.0")
# Check that URL uses tags path for tag version
expected_url = (
"https://raw.githubusercontent.com/omsf/static-site-tools/"
"refs/tags/v2.1.0/.github/workflows/example-jekyll-build-pr.yaml"
)
first_call_args, _ = mock_download.call_args_list[0]
first_call_url = first_call_args[0]
self.assertEqual(first_call_url, expected_url)
update_args, _ = mock_update.call_args_list[0]
self.assertEqual(update_args[2], "v2.1.0")
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_branch_version(self, mock_download, mock_edit, mock_update):
"""Test install function with branch version."""
install("hugo", "branch", "main")
# Check that URL uses heads/main path for branch version
expected_url = (
"https://raw.githubusercontent.com/omsf/static-site-tools/"
"refs/heads/main/.github/workflows/example-hugo-build-pr.yaml"
)
first_call_args, _ = mock_download.call_args_list[0]
first_call_url = first_call_args[0]
self.assertEqual(first_call_url, expected_url)
update_args, _ = mock_update.call_args_list[0]
self.assertEqual(update_args[2], "main")
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_commit_version(self, mock_download, mock_edit, mock_update):
"""Test install function with commit version."""
install("astro", "commit", "abc123def456")
# Check that URL uses commit hash directly
expected_url = (
"https://raw.githubusercontent.com/omsf/static-site-tools/"
"abc123def456/.github/workflows/example-astro-build-pr.yaml"
)
first_call_args, _ = mock_download.call_args_list[0]
first_call_url = first_call_args[0]
self.assertEqual(first_call_url, expected_url)
update_args, _ = mock_update.call_args_list[0]
self.assertEqual(update_args[2], "abc123def456")
@patch("install.update_reusable_workflow_references")
@patch("install.edit_source_directory")
@patch("install.download_file")
def test_install_custom_branch(self, mock_download, mock_edit, mock_update):
"""Test install function with custom branch."""
install("hugo", "branch", "develop")
# Check that URL uses heads/develop path for custom branch
expected_url = (
"https://raw.githubusercontent.com/omsf/static-site-tools/"
"refs/heads/develop/.github/workflows/example-hugo-build-pr.yaml"
)
first_call_args, _ = mock_download.call_args_list[0]
first_call_url = first_call_args[0]
self.assertEqual(first_call_url, expected_url)
update_args, _ = mock_update.call_args_list[0]
self.assertEqual(update_args[2], "develop")
class TestIntegration(unittest.TestCase):
"""Integration tests for the complete workflow."""
def setUp(self):
self.temp_dir = tempfile.mkdtemp()
self.addCleanup(shutil.rmtree, self.temp_dir)
@patch("install.urllib.request.urlretrieve")
def test_full_workflow_simulation(self, mock_urlretrieve):
"""Test the complete workflow with mocked HTTP requests."""
commit_sha = "abcdef1234567890"
# Mock file content that would be downloaded
mock_content = """name: Build Hugo site from PR
env:
HUGO_SOURCE_DIR: "example-hugo"
HUGO_BASE_URL: ""
jobs:
build:
steps:
- uses: omsf/static-site-tools/build/hugo@main
"""
def mock_retrieve(url, destination):
with open(destination, "w") as f:
f.write(mock_content)
mock_urlretrieve.side_effect = mock_retrieve
output_dir = pathlib.Path(self.temp_dir) / "workflows"
with patch("install.print"):
install(
"hugo",
"commit",
commit_sha,
str(output_dir),
"my-hugo-site",
"omsf/static-site-tools",
)
# Check that files were created and edited
build_pr_file = output_dir / "build-pr.yaml"
self.assertTrue(build_pr_file.exists())
with open(build_pr_file, "r") as f:
content = f.read()
# Verify that the content was properly edited
self.assertIn('HUGO_SOURCE_DIR: "my-hugo-site"', content)
self.assertNotIn('HUGO_SOURCE_DIR: "example-hugo"', content)
self.assertIn(f"omsf/static-site-tools/build/hugo@{commit_sha}", content)
self.assertNotIn("omsf/static-site-tools/build/hugo@main", content)
if __name__ == "__main__":
# Run the tests
unittest.main(verbosity=2)