|
| 1 | +# SPDX-FileCopyrightText: 2025-present Datadog, Inc. <dev@datadoghq.com> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: MIT |
| 4 | +from __future__ import annotations |
| 5 | + |
| 6 | +import platform |
| 7 | + |
| 8 | +import pytest |
| 9 | + |
| 10 | +from dda.utils.fs import Path |
| 11 | + |
| 12 | + |
| 13 | +def test_default(app): |
| 14 | + with app.tools.go.execution_context([]) as context: |
| 15 | + assert context.env_vars == {} |
| 16 | + |
| 17 | + |
| 18 | +class TestPrecedence: |
| 19 | + def test_workspace_file(self, app, temp_dir): |
| 20 | + (temp_dir / "go.work").write_text("stuff\ngo X.Y.Z\nstuff") |
| 21 | + with temp_dir.as_cwd(), app.tools.go.execution_context([]) as context: |
| 22 | + assert context.env_vars == {"GOTOOLCHAIN": "goX.Y.Z"} |
| 23 | + |
| 24 | + def test_module_file(self, app, temp_dir): |
| 25 | + (temp_dir / "go.work").write_text("stuff\ngo X.Y.Z\nstuff") |
| 26 | + (temp_dir / "go.mod").write_text("stuff\ngo X.Y.Zrc1\nstuff") |
| 27 | + with temp_dir.as_cwd(), app.tools.go.execution_context([]) as context: |
| 28 | + assert context.env_vars == {"GOTOOLCHAIN": "goX.Y.Zrc1"} |
| 29 | + |
| 30 | + def test_version_file(self, app, temp_dir): |
| 31 | + (temp_dir / "go.work").write_text("stuff\ngo X.Y.Z\nstuff") |
| 32 | + (temp_dir / "go.mod").write_text("stuff\ngo X.Y.Zrc1\nstuff") |
| 33 | + (temp_dir / ".go-version").write_text("X.Y.Zrc2") |
| 34 | + with temp_dir.as_cwd(), app.tools.go.execution_context([]) as context: |
| 35 | + assert context.env_vars == {"GOTOOLCHAIN": "goX.Y.Zrc2"} |
| 36 | + |
| 37 | + |
| 38 | +class TestBuild: |
| 39 | + @pytest.mark.parametrize( |
| 40 | + "call_args", |
| 41 | + [ |
| 42 | + {"n_packages": 2}, |
| 43 | + {"n_packages": 1, "build_tags": {"debug"}}, |
| 44 | + {"n_packages": 0, "build_tags": {"prod"}, "gcflags": ["all=-N -l"], "ldflags": ["all=-s -w", "-dumpdep"]}, |
| 45 | + {"n_packages": 2, "build_tags": {"prod"}, "gcflags": ["all=-N -l"], "ldflags": ["all=-s -w", "-dumpdep"]}, |
| 46 | + {"n_packages": 0, "force_rebuild": True}, |
| 47 | + ], |
| 48 | + ) |
| 49 | + def test_command_formation(self, app, mocker, call_args, get_random_filename): |
| 50 | + # Patch the raw _build method to avoid running anything |
| 51 | + mocker.patch("dda.tools.go.Go._build", return_value="output") |
| 52 | + |
| 53 | + # Generate dummy package and output paths |
| 54 | + n_packages = call_args.pop("n_packages", 0) |
| 55 | + packages: tuple[str, ...] = tuple(get_random_filename() for _ in range(n_packages)) |
| 56 | + output: Path = get_random_filename() |
| 57 | + app.tools.go.build( |
| 58 | + *packages, |
| 59 | + output=output, |
| 60 | + **call_args, |
| 61 | + ) |
| 62 | + |
| 63 | + flags = { |
| 64 | + ("-trimpath",), |
| 65 | + ("-mod=readonly",), |
| 66 | + (f"-o={output}",), |
| 67 | + # ("-v",), # By default verbosity is INFO |
| 68 | + # ("-x",), |
| 69 | + } |
| 70 | + |
| 71 | + if not (platform.system() == "Windows" and platform.machine() == "arm64"): |
| 72 | + flags.add(("-race",)) |
| 73 | + |
| 74 | + if call_args.get("build_tags"): |
| 75 | + flags.add(("-tags", ",".join(sorted(call_args.get("build_tags", []))))) |
| 76 | + if call_args.get("gcflags"): |
| 77 | + flags.add((f"-gcflags={' '.join(call_args.get('gcflags'))}",)) |
| 78 | + if call_args.get("ldflags"): |
| 79 | + flags.add((f"-ldflags={' '.join(call_args.get('ldflags'))}",)) |
| 80 | + if call_args.get("force_rebuild"): |
| 81 | + flags.add(("-a",)) |
| 82 | + |
| 83 | + seen_command_parts = app.tools.go._build.call_args[0][0] # noqa: SLF001 |
| 84 | + |
| 85 | + flags_len = len(flags) |
| 86 | + seen_flags: list[str] = seen_command_parts[: flags_len + 1] |
| 87 | + for flag_tuple in flags: |
| 88 | + assert flag_tuple[0] in seen_flags |
| 89 | + |
| 90 | + if len(flag_tuple) > 1: |
| 91 | + flag_index = seen_flags.index(flag_tuple[0]) |
| 92 | + assert seen_flags[flag_index + 1] == flag_tuple[1] |
| 93 | + |
| 94 | + if n_packages > 0: |
| 95 | + assert seen_command_parts[-len(packages) :] == [str(package) for package in packages] |
| 96 | + |
| 97 | + # This test is quite slow, we'll only run it in CI |
| 98 | + @pytest.mark.requires_ci |
| 99 | + @pytest.mark.skip_macos # Go binary is not installed on macOS CI runners |
| 100 | + def test_build_project(self, app, temp_dir): |
| 101 | + for tag, output_mark in [("prod", "PRODUCTION"), ("debug", "DEBUG")]: |
| 102 | + with (Path(__file__).parent / "fixtures" / "small_go_project").as_cwd(): |
| 103 | + app.tools.go.build( |
| 104 | + ".", |
| 105 | + output=(temp_dir / "testbinary").absolute(), |
| 106 | + build_tags={tag}, |
| 107 | + force_rebuild=True, |
| 108 | + ) |
| 109 | + |
| 110 | + assert (temp_dir / "testbinary").is_file() |
| 111 | + output = app.subprocess.capture(str(temp_dir / "testbinary")) |
| 112 | + assert output_mark in output |
| 113 | + # Note: doing both builds in the same test with the same name also allows us to test the force rebuild |
0 commit comments