|
| 1 | +# |
| 2 | +# Copyright (C) 2020-2026 Arm Limited or its affiliates and Contributors. All rights reserved. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# |
| 5 | +import shutil |
| 6 | +from pathlib import Path |
| 7 | +from unittest import TestCase, mock |
| 8 | +from unittest import skipUnless |
| 9 | + |
| 10 | +from continuous_delivery_scripts.plugins import golang |
| 11 | +from continuous_delivery_scripts.utils.filesystem_helpers import TemporaryDirectory |
| 12 | + |
| 13 | +GO_AVAILABLE = shutil.which("go") is not None |
| 14 | + |
| 15 | + |
| 16 | +class TestGoModuleTags(TestCase): |
| 17 | + def test_determine_go_module_tag_keeps_current_module(self): |
| 18 | + with TemporaryDirectory() as temp_dir: |
| 19 | + root = Path(temp_dir) |
| 20 | + source_dir = root.joinpath("src") |
| 21 | + source_dir.mkdir() |
| 22 | + |
| 23 | + with mock.patch.object(golang, "ROOT_DIR", root), mock.patch.object(golang, "SRC_DIR", source_dir): |
| 24 | + tags = golang._determine_go_module_tag("v1.2.3") |
| 25 | + |
| 26 | + self.assertEqual(tags, ["src/v1.2.3"]) |
| 27 | + |
| 28 | + def test_determine_go_module_tag_reads_go_work_modules(self): |
| 29 | + with TemporaryDirectory() as temp_dir: |
| 30 | + root = Path(temp_dir) |
| 31 | + source_dir = root.joinpath("src") |
| 32 | + source_dir.mkdir() |
| 33 | + root.joinpath("go.work").touch() |
| 34 | + |
| 35 | + with ( |
| 36 | + mock.patch.object(golang, "ROOT_DIR", root), |
| 37 | + mock.patch.object(golang, "SRC_DIR", source_dir), |
| 38 | + mock.patch.object( |
| 39 | + golang, |
| 40 | + "check_output", |
| 41 | + return_value='{"Use": [{"DiskPath": "./app1"}, {"DiskPath": "./nested/app2"}]}', |
| 42 | + ), |
| 43 | + ): |
| 44 | + tags = golang._determine_go_module_tag("v1.2.3") |
| 45 | + |
| 46 | + self.assertEqual(tags, ["src/v1.2.3", "app1/v1.2.3", "nested/app2/v1.2.3"]) |
| 47 | + |
| 48 | + def test_determine_go_module_tag_reads_go_work_from_source_dir(self): |
| 49 | + with TemporaryDirectory() as temp_dir: |
| 50 | + root = Path(temp_dir) |
| 51 | + source_dir = root.joinpath("src") |
| 52 | + source_dir.mkdir() |
| 53 | + source_dir.joinpath("go.work").touch() |
| 54 | + |
| 55 | + with ( |
| 56 | + mock.patch.object(golang, "ROOT_DIR", root), |
| 57 | + mock.patch.object(golang, "SRC_DIR", source_dir), |
| 58 | + mock.patch.object( |
| 59 | + golang, |
| 60 | + "check_output", |
| 61 | + return_value='{"Use": [{"DiskPath": "./app1"}, {"DiskPath": "./nested/app2"}]}', |
| 62 | + ), |
| 63 | + ): |
| 64 | + tags = golang._determine_go_module_tag("v1.2.3") |
| 65 | + |
| 66 | + self.assertEqual(tags, ["src/v1.2.3", "src/app1/v1.2.3", "src/nested/app2/v1.2.3"]) |
| 67 | + |
| 68 | + def test_determine_go_module_tag_reads_multiple_go_work_files(self): |
| 69 | + with TemporaryDirectory() as temp_dir: |
| 70 | + root = Path(temp_dir) |
| 71 | + source_dir = root.joinpath("src") |
| 72 | + source_dir.mkdir() |
| 73 | + root.joinpath("go.work").touch() |
| 74 | + source_dir.joinpath("go.work").touch() |
| 75 | + |
| 76 | + def check_output_side_effect(command, cwd=None, encoding=None): |
| 77 | + if Path(str(cwd)) == source_dir: |
| 78 | + return '{"Use": [{"DiskPath": "./app1"}]}' |
| 79 | + if Path(str(cwd)) == root: |
| 80 | + return '{"Use": [{"DiskPath": "./shared"}, {"DiskPath": "./root-app"}]}' |
| 81 | + raise AssertionError(f"Unexpected cwd: {cwd}") |
| 82 | + |
| 83 | + with ( |
| 84 | + mock.patch.object(golang, "ROOT_DIR", root), |
| 85 | + mock.patch.object(golang, "SRC_DIR", source_dir), |
| 86 | + mock.patch.object(golang, "check_output", side_effect=check_output_side_effect), |
| 87 | + ): |
| 88 | + tags = golang._determine_go_module_tag("v1.2.3") |
| 89 | + |
| 90 | + self.assertEqual(tags, ["src/v1.2.3", "src/app1/v1.2.3", "shared/v1.2.3", "root-app/v1.2.3"]) |
| 91 | + |
| 92 | + def test_determine_go_module_tag_reads_nested_go_mod_projects(self): |
| 93 | + with TemporaryDirectory() as temp_dir: |
| 94 | + root = Path(temp_dir) |
| 95 | + source_dir = root.joinpath("src") |
| 96 | + source_dir.mkdir() |
| 97 | + source_dir.joinpath("go.mod").write_text("module example.com/src\n", encoding="utf8") |
| 98 | + source_dir.joinpath("service-a").mkdir() |
| 99 | + source_dir.joinpath("service-a", "go.mod").write_text("module example.com/service-a\n", encoding="utf8") |
| 100 | + source_dir.joinpath("service-b").mkdir() |
| 101 | + source_dir.joinpath("service-b", "go.mod").write_text("module example.com/service-b\n", encoding="utf8") |
| 102 | + |
| 103 | + with mock.patch.object(golang, "ROOT_DIR", root), mock.patch.object(golang, "SRC_DIR", source_dir): |
| 104 | + tags = golang._determine_go_module_tag("v1.2.3") |
| 105 | + |
| 106 | + self.assertEqual(tags, ["src/v1.2.3", "src/service-a/v1.2.3", "src/service-b/v1.2.3"]) |
| 107 | + |
| 108 | + |
| 109 | +@skipUnless(GO_AVAILABLE, "go command is required for this integration test") |
| 110 | +class TestGoWorkIntegration(TestCase): |
| 111 | + def test_determine_go_work_module_directories_from_json_with_go(self): |
| 112 | + with TemporaryDirectory() as temp_dir: |
| 113 | + root = Path(temp_dir) |
| 114 | + root.joinpath("go.work").write_text("go 1.22\n\nuse ./app1\n", encoding="utf8") |
| 115 | + root.joinpath("app1").mkdir() |
| 116 | + |
| 117 | + directories = golang._determine_go_work_module_directories_from_json(root.joinpath("go.work")) |
| 118 | + |
| 119 | + self.assertEqual(directories, [root.joinpath("app1")]) |
0 commit comments