-
Notifications
You must be signed in to change notification settings - Fork 3
ACIX-1062: Create helper function for go build
#211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
3714ef4
feat(go): Add a custom `Go.build` method for easier invocations of `g…
Ishirui e2434ca
fix(go): Make the `-a` argument controllable
Ishirui fea5456
fix(build): Prevent `get_random_filename` from creating filenames sta…
Ishirui ac5fde0
feat(tests): Add `skip_${OS}` pytest markers for skipping tests on sp…
Ishirui 37748c8
fix(tests): Skip `TestBuild::test_build_project` on macOS as `go` is …
Ishirui 23cc251
fix(go): Tweak `Go.build` method interface after testing
Ishirui 49829e2
refactor: Minor Copilot-suggested tweaks
Ishirui a70eed8
refactor: Simplify `get_random_filename` test fixture
Ishirui 946a62f
refactor(go): Pass `*packages` instead of a single `entrypoint`
Ishirui 86f06c8
refactor(tests): Use sets of build tags everywhere
Ishirui 8ba515b
feat(go): Allow passing 0 packages to `build` method
Ishirui f16be01
refactor(tests): `get_random_filename` returns a simple `str`
Ishirui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| # SPDX-FileCopyrightText: 2025-present Datadog, Inc. <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: MIT |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| from base64 import urlsafe_b64encode | ||
| from os import urandom | ||
|
|
||
| import pytest | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def get_random_filename(): | ||
Ishirui marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| def _get_random_filename(k: int = 10) -> str: | ||
| return urlsafe_b64encode(urandom(k)).decode("utf-8") | ||
|
|
||
| return _get_random_filename | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //go:build debug | ||
| // +build debug | ||
|
|
||
| package main | ||
|
|
||
| // Also defined in prod.go to test build tags | ||
| func TagInfo() string { | ||
| return "DEBUG" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| module github.com/DataDog/datadog-agent-dev/tests/tools/go/fixtures/small_go_project | ||
|
|
||
| go 1.21.0 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "os" | ||
| "runtime" | ||
| ) | ||
|
|
||
| func main() { | ||
| fmt.Printf("Hello from small go project!\n") | ||
| fmt.Printf("Go version: %s\n", runtime.Version()) | ||
| fmt.Printf("OS/Arch: %s/%s\n", runtime.GOOS, runtime.GOARCH) | ||
|
|
||
| // Test command line args | ||
| if len(os.Args) > 1 { | ||
| fmt.Printf("Arguments: %v\n", os.Args[1:]) | ||
| } | ||
|
|
||
| // Call build-specific function to test build tags | ||
| fmt.Printf("Tag: %s\n", TagInfo()) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| //go:build !debug | ||
| // +build !debug | ||
|
|
||
| package main | ||
|
|
||
| // Also defined in debug.go to test build tags | ||
| func TagInfo() string { | ||
| return "PRODUCTION" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # SPDX-FileCopyrightText: 2025-present Datadog, Inc. <[email protected]> | ||
| # | ||
| # SPDX-License-Identifier: MIT | ||
| from __future__ import annotations | ||
|
|
||
| import platform | ||
|
|
||
| import pytest | ||
|
|
||
| from dda.utils.fs import Path | ||
|
|
||
|
|
||
| def test_default(app): | ||
| with app.tools.go.execution_context([]) as context: | ||
| assert context.env_vars == {} | ||
|
|
||
|
|
||
| class TestPrecedence: | ||
| def test_workspace_file(self, app, temp_dir): | ||
| (temp_dir / "go.work").write_text("stuff\ngo X.Y.Z\nstuff") | ||
| with temp_dir.as_cwd(), app.tools.go.execution_context([]) as context: | ||
| assert context.env_vars == {"GOTOOLCHAIN": "goX.Y.Z"} | ||
|
|
||
| def test_module_file(self, app, temp_dir): | ||
| (temp_dir / "go.work").write_text("stuff\ngo X.Y.Z\nstuff") | ||
| (temp_dir / "go.mod").write_text("stuff\ngo X.Y.Zrc1\nstuff") | ||
| with temp_dir.as_cwd(), app.tools.go.execution_context([]) as context: | ||
| assert context.env_vars == {"GOTOOLCHAIN": "goX.Y.Zrc1"} | ||
|
|
||
| def test_version_file(self, app, temp_dir): | ||
| (temp_dir / "go.work").write_text("stuff\ngo X.Y.Z\nstuff") | ||
| (temp_dir / "go.mod").write_text("stuff\ngo X.Y.Zrc1\nstuff") | ||
| (temp_dir / ".go-version").write_text("X.Y.Zrc2") | ||
| with temp_dir.as_cwd(), app.tools.go.execution_context([]) as context: | ||
| assert context.env_vars == {"GOTOOLCHAIN": "goX.Y.Zrc2"} | ||
|
|
||
|
|
||
| class TestBuild: | ||
| @pytest.mark.parametrize( | ||
| "call_args", | ||
| [ | ||
| {"n_packages": 2}, | ||
| {"n_packages": 1, "build_tags": {"debug"}}, | ||
| {"n_packages": 0, "build_tags": {"prod"}, "gcflags": ["all=-N -l"], "ldflags": ["all=-s -w", "-dumpdep"]}, | ||
| {"n_packages": 2, "build_tags": {"prod"}, "gcflags": ["all=-N -l"], "ldflags": ["all=-s -w", "-dumpdep"]}, | ||
| {"n_packages": 0, "force_rebuild": True}, | ||
| ], | ||
| ) | ||
| def test_command_formation(self, app, mocker, call_args, get_random_filename): | ||
| # Patch the raw _build method to avoid running anything | ||
| mocker.patch("dda.tools.go.Go._build", return_value="output") | ||
|
|
||
| # Generate dummy package and output paths | ||
| n_packages = call_args.pop("n_packages", 0) | ||
| packages: tuple[str, ...] = tuple(get_random_filename() for _ in range(n_packages)) | ||
| output: Path = get_random_filename() | ||
| app.tools.go.build( | ||
| *packages, | ||
| output=output, | ||
| **call_args, | ||
| ) | ||
|
|
||
| flags = { | ||
| ("-trimpath",), | ||
| ("-mod=readonly",), | ||
| (f"-o={output}",), | ||
| # ("-v",), # By default verbosity is INFO | ||
| # ("-x",), | ||
| } | ||
|
|
||
| if not (platform.system() == "Windows" and platform.machine() == "arm64"): | ||
| flags.add(("-race",)) | ||
|
|
||
| if call_args.get("build_tags"): | ||
| flags.add(("-tags", ",".join(sorted(call_args.get("build_tags", []))))) | ||
| if call_args.get("gcflags"): | ||
| flags.add((f"-gcflags={' '.join(call_args.get('gcflags'))}",)) | ||
| if call_args.get("ldflags"): | ||
| flags.add((f"-ldflags={' '.join(call_args.get('ldflags'))}",)) | ||
| if call_args.get("force_rebuild"): | ||
| flags.add(("-a",)) | ||
|
|
||
| seen_command_parts = app.tools.go._build.call_args[0][0] # noqa: SLF001 | ||
|
|
||
| flags_len = len(flags) | ||
| seen_flags: list[str] = seen_command_parts[: flags_len + 1] | ||
| for flag_tuple in flags: | ||
| assert flag_tuple[0] in seen_flags | ||
|
|
||
| if len(flag_tuple) > 1: | ||
| flag_index = seen_flags.index(flag_tuple[0]) | ||
| assert seen_flags[flag_index + 1] == flag_tuple[1] | ||
|
|
||
| if n_packages > 0: | ||
| assert seen_command_parts[-len(packages) :] == [str(package) for package in packages] | ||
|
|
||
| # This test is quite slow, we'll only run it in CI | ||
| @pytest.mark.requires_ci | ||
| @pytest.mark.skip_macos # Go binary is not installed on macOS CI runners | ||
| def test_build_project(self, app, temp_dir): | ||
| for tag, output_mark in [("prod", "PRODUCTION"), ("debug", "DEBUG")]: | ||
| with (Path(__file__).parent / "fixtures" / "small_go_project").as_cwd(): | ||
| app.tools.go.build( | ||
| ".", | ||
| output=(temp_dir / "testbinary").absolute(), | ||
| build_tags={tag}, | ||
| force_rebuild=True, | ||
| ) | ||
|
|
||
| assert (temp_dir / "testbinary").is_file() | ||
| output = app.subprocess.capture(str(temp_dir / "testbinary")) | ||
| assert output_mark in output | ||
| # Note: doing both builds in the same test with the same name also allows us to test the force rebuild |
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.