Skip to content

Add GO_TESTFLAGS variable to append to test flags#352

Merged
majewsky merged 7 commits intomainfrom
add_test_args
Sep 2, 2025
Merged

Add GO_TESTFLAGS variable to append to test flags#352
majewsky merged 7 commits intomainfrom
add_test_args

Conversation

@stanislav-zaprudskiy
Copy link
Copy Markdown
Contributor

@stanislav-zaprudskiy stanislav-zaprudskiy commented Aug 21, 2025

The PR adds a new variable GO_TESTFLAGS for Makefile to manipulate additional flags to append to the test command. For example:

variables:
  GO_TESTFLAGS: '-short -v -count=10'

would append the respective arguments to the test command after all other provided test flags (provided by go-makefile-maker).

This could be useful when one needs to alter default go-makefile-maker behavior, for example:

  • make the default test target execute short tests
  • increase verbosity of test execution using -v

and so on.

The appended variable should work for both go test and ginkgo - where it appends just before packages specification:

go test [build/test flags] [packages] [build/test flags & test binary flags]
ginkgo run <FLAGS> <PACKAGES> -- <PASS-THROUGHS>

See also #352 (comment) thread for additional considerations around the behavior.

if isGolang {
build.addDefinition("GO_BUILDFLAGS =%s", cfg.Variable("GO_BUILDFLAGS", defaultBuildFlags))
build.addDefinition("GO_LDFLAGS =%s", cfg.Variable("GO_LDFLAGS", strings.TrimSpace(defaultLdFlags)))
build.addDefinition("GO_TESTFLAGS :=%s $(GO_TESTFLAGS)", cfg.Variable("GO_TESTFLAGS", ""))
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's using slightly different approach here for the assignment to accommodate opportunities to both set and append to the variable.

Given the following configuration:

variables:
  GO_TESTFLAGS: '-short'
verbatim: |
  foo:
    go test $(GO_TESTFLAGS) pkg.example/foo

A normal target call would render:

make foo
go test -short  pkg.example/foo

In order to append test flags one would do:

GO_TESTFLAGS="-v -count=1" make foo
go test -short -v -count=1 pkg.example/foo

In order to override test flags one would do:

make foo GO_TESTFLAGS=
go test  pkg.example/foo

(note, the new target foo is merely for representation purpose - the introduced variable is used in the main test command target).

If the proposed behavior is fine, I'd add a paragraph to describe it in README. It may be reasonable to also add the same for other variables - though this would backward incompatible with previous behavior, so I'm open for your suggestions.

Copy link
Copy Markdown
Contributor

@majewsky majewsky Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked in my workgroup; we don't see a strong reason for why this should not be allowed. (Maybe this will blow up somewhere because it does break backwards compatibility, but we're willing to risk that.) Please apply this scheme to all variables: and document accordingly.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if isGolang {
build.addDefinition("GO_BUILDFLAGS =%s", cfg.Variable("GO_BUILDFLAGS", defaultBuildFlags))
build.addDefinition("GO_LDFLAGS =%s", cfg.Variable("GO_LDFLAGS", strings.TrimSpace(defaultLdFlags)))
build.addDefinition("GO_TESTFLAGS :=%s $(GO_TESTFLAGS)", cfg.Variable("GO_TESTFLAGS", ""))
Copy link
Copy Markdown
Contributor

@majewsky majewsky Aug 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked in my workgroup; we don't see a strong reason for why this should not be allowed. (Maybe this will blow up somewhere because it does break backwards compatibility, but we're willing to risk that.) Please apply this scheme to all variables: and document accordingly.

majewsky
majewsky previously approved these changes Aug 29, 2025
@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Sep 1, 2025

Merging this branch will decrease overall coverage

Impacted Packages Coverage Δ 🤖
github.com/sapcc/go-makefile-maker/internal/makefile 8.05% (-0.02%) 👎

Coverage by file

Changed files (no unit tests)

Changed File Coverage Δ Total Covered Missed 🤖
github.com/sapcc/go-makefile-maker/internal/makefile/makefile.go 0.00% (ø) 194 (+1) 0 194 (+1)

Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code.

@majewsky majewsky merged commit 2f2c572 into main Sep 2, 2025
8 checks passed
@majewsky majewsky deleted the add_test_args branch September 2, 2025 09:02
majewsky added a commit that referenced this pull request Sep 4, 2025
The override mechanism proposed by @stanislav-zaprudskiy in #352 is
problematic because --warn-undefined-variables makes noise about it:

```shellSession
$ make
Makefile:67: warning: undefined variable 'GO_BUILDFLAGS'
Makefile:68: warning: undefined variable 'GO_LDFLAGS'
Makefile:69: warning: undefined variable 'GO_TESTFLAGS'
Makefile:70: warning: undefined variable 'GO_TESTENV'
Makefile:71: warning: undefined variable 'GO_BUILDENV'
env  go build -mod vendor  -ldflags '-s -w ...
```

However, through much experimentation, I discovered that the += operator
behaves in the intended way, as documented in the new generated comment.
majewsky added a commit that referenced this pull request Sep 4, 2025
The override mechanism proposed by @stanislav-zaprudskiy in #352 is
problematic because --warn-undefined-variables makes noise about it:

```shellSession
$ make
Makefile:67: warning: undefined variable 'GO_BUILDFLAGS'
Makefile:68: warning: undefined variable 'GO_LDFLAGS'
Makefile:69: warning: undefined variable 'GO_TESTFLAGS'
Makefile:70: warning: undefined variable 'GO_TESTENV'
Makefile:71: warning: undefined variable 'GO_BUILDENV'
env  go build -mod vendor  -ldflags '-s -w ...
```

However, through much experimentation, I discovered that the += operator
behaves in the intended way, as documented in the new generated comment.
majewsky added a commit that referenced this pull request Sep 4, 2025
The override mechanism proposed by @stanislav-zaprudskiy in #352 is
problematic because --warn-undefined-variables makes noise about it:

```shellSession
$ make
Makefile:67: warning: undefined variable 'GO_BUILDFLAGS'
Makefile:68: warning: undefined variable 'GO_LDFLAGS'
Makefile:69: warning: undefined variable 'GO_TESTFLAGS'
Makefile:70: warning: undefined variable 'GO_TESTENV'
Makefile:71: warning: undefined variable 'GO_BUILDENV'
env  go build -mod vendor  -ldflags '-s -w ...
```

However, through much experimentation, I discovered that the += operator
behaves in the intended way, as documented in the new generated comment.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants