1
0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2026-10-23 04:48:57 +03:00
Files
runner/testutils/test_main.go
Andreas Ahlenstorf 9a617b497b test: use flags instead of env variables for skipping tests (#1591)
https://code.forgejo.org/forgejo/runner/pulls/1585 introduced the environment variable `TEST_FEATURES` for switching tests related to particular feature on and off. For example, with `TEST_FEATURES=""`, tests that require Docker would be skipped. Unfortunately, that technique has multiple problems. Environment variables might not be passed on to child processes without it being clearly visible. Also, the usage differs by platform. On top of that, informational messages that are only related to testing were printed by the release binary.

Instead of the environment variable `TEST_FEATURES`, the command line flag `-features` is introduced:

```
$  go test ./... -args -features "docker,lxc"
```

That runs all tests that require the presence of Docker and LXC. It is also the default.

If tests that require Docker should be skipped, run:

```
$  go test ./... -args -features "lxc"
```

To exclude all tests that require the presence of a particular feature, run:

```
$  go test ./... -args -features "-"
```

At the moment, `-features` cannot be controlled when using make targets. That means that `make integration-test` runs all tests that require Docker or LXC.

Supersedes  https://code.forgejo.org/forgejo/runner/pulls/1588.

<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- other
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/1591): <!--number 1591 --><!--line 0 --><!--description dGVzdDogdXNlIGZsYWdzIGluc3RlYWQgb2YgZW52IHZhcmlhYmxlcyBmb3Igc2tpcHBpbmcgdGVzdHM=-->test: use flags instead of env variables for skipping tests<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/1591
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org>
2026-07-05 22:26:52 +00:00

76 lines
1.8 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package testutils
import (
"flag"
"fmt"
"strings"
"testing"
)
type TestFeature string
const (
// TestFeatureDocker enables tests that exercise functionality that requires Docker or Podman.
TestFeatureDocker TestFeature = "docker"
// TestFeatureLXC enables tests that exercise functionality that requires LXC.
TestFeatureLXC TestFeature = "lxc"
)
type featureList map[TestFeature]struct{}
var enabledTestFeatures featureList = map[TestFeature]struct{}{
TestFeatureDocker: {},
TestFeatureLXC: {},
}
func (f *featureList) String() string {
features := make([]string, 0, len(*f))
for feature := range *f {
features = append(features, string(feature))
}
return strings.Join(features, ",")
}
func (f *featureList) Set(value string) error {
enabledTestFeatures = map[TestFeature]struct{}{}
requestedFeatures := strings.Split(value, ",")
for _, requestedFeature := range requestedFeatures {
switch strings.TrimSpace(requestedFeature) {
case string(TestFeatureDocker):
(*f)[TestFeatureDocker] = struct{}{}
case string(TestFeatureLXC):
(*f)[TestFeatureLXC] = struct{}{}
case "-":
for key := range *f {
delete(*f, key)
}
return nil
default:
return fmt.Errorf("unknown feature: %q", value)
}
}
return nil
}
func init() {
flag.Var(&enabledTestFeatures, "features",
"comma-separated list of feature whose tests should be included, \"-\" for none")
}
// RequireTestFeatures skips a test if not all the given TestFeature instances are enabled.
func RequireTestFeatures(t *testing.T, requiredFeatures ...TestFeature) {
t.Helper()
for _, requiredFeature := range requiredFeatures {
if _, exists := enabledTestFeatures[requiredFeature]; !exists {
t.Skipf("Tests of feature %q are disabled", requiredFeature)
}
}
}