Skip to content

Commit

Permalink
Merge pull request #4537 from grafana/refactor-lib-consts
Browse files Browse the repository at this point in the history
Move constants in lib package to a dedicated and internal package.
  • Loading branch information
codebien authored Feb 20, 2025
2 parents 79ba29f + 4d9babe commit 19a4eae
Show file tree
Hide file tree
Showing 24 changed files with 64 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .github/ISSUE_TEMPLATE/release.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ labels: ["release"]
- [ ] Ask teams that might have contributed to the release (e.g., k6-ecosystem, k6-docs, k6-devrel teams) to contribute their notes and review the existing ones.
- [ ] Remember to mention and thank [external contributors](https://github.com/search?q=user%3Agrafana+repo%3Ak6+milestone%3A%22v0.51.0%22+-author%3Amstoykov+-author%3Aoleiade+-author%3Ana--+-author%3Acodebien+-author%3Aolegbespalov+-author%3Aandrewslotin+-author%3Ajoanlopez+-author%3Aankur22+-author%3Ainancgumus+-author%3Aszkiba+-author%3Adependabot%5Bbot%5D&type=pullrequests). (**<- Update the query with the correct milestone version**).
- [ ] Share the release notes PR with the k6 open-source teams. Request contributions from all affected teams (k6-chaos, k6-docs, k6-devrel, etc.) and any other stakeholders involved in the new release.
- [ ] Open a separate PR for bumping [the k6 Go project's version](https://github.com/grafana/k6/blob/master/lib/consts/consts.go#L11-L12).
- [ ] Open a separate PR for bumping [the k6 Go project's version](https://github.com/grafana/k6/blob/master/internal/build/version.go#L6).
- [ ] Open a PR in the `DefinitelyTyped/DefinitelyTyped` repository using the release branch created in the grafana/k6-DefinitelyTyped fork to update the k6 type definitions for the new release.

#### ~ 1 day before the release date
Expand Down
5 changes: 2 additions & 3 deletions cloudapi/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@ import (

"go.k6.io/k6/lib/fsext"

"go.k6.io/k6/lib/consts"

"go.k6.io/k6/lib"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/lib/testutils"
"go.k6.io/k6/lib/types"
)
Expand Down Expand Up @@ -111,7 +110,7 @@ func TestCreateTestRun(t *testing.T) {

arc := &lib.Archive{
Type: "js",
K6Version: consts.Version,
K6Version: build.Version,
Options: lib.Options{},
FilenameURL: &url.URL{Scheme: "file", Path: "/path/to/a.js"},
Data: []byte(`// a contents`),
Expand Down
6 changes: 6 additions & 0 deletions internal/build/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Package build handles information and processes about the k6 binary's build.
package build

// Version contains the current version of k6
// represented using Semantic Versioning expression.
const Version = "0.57.0"
4 changes: 2 additions & 2 deletions internal/cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import (
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/ui/pb"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"

"github.com/fatih/color"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -164,7 +164,7 @@ func (c *cmdCloud) run(cmd *cobra.Command, args []string) error {
// Start cloud test run
modifyAndPrintBar(c.gs, progressBar, pb.WithConstProgress(0, "Validating script options"))
client := cloudapi.NewClient(
logger, cloudConfig.Token.String, cloudConfig.Host.String, consts.Version, cloudConfig.Timeout.TimeDuration())
logger, cloudConfig.Token.String, cloudConfig.Host.String, build.Version, cloudConfig.Timeout.TimeDuration())
if err = client.ValidateOptions(arc.Options); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/cloud_login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/ui"
"go.k6.io/k6/lib/consts"
)

const cloudLoginCommandName = "login"
Expand Down Expand Up @@ -168,7 +168,7 @@ func validateToken(gs *state.GlobalState, jsonRawConf json.RawMessage, token str
gs.Logger,
token,
consolidatedCurrentConfig.Host.String,
consts.Version,
build.Version,
consolidatedCurrentConfig.Timeout.TimeDuration(),
)

Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/login_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/ui"
"go.k6.io/k6/lib/consts"
)

//nolint:funlen,gocognit
Expand Down Expand Up @@ -114,7 +114,7 @@ Please use the "k6 cloud login" command instead.
gs.Logger,
"",
consolidatedCurrentConfig.Host.String,
consts.Version,
build.Version,
consolidatedCurrentConfig.Timeout.TimeDuration())

var res *cloudapi.LoginResponse
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (
"github.com/spf13/pflag"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/internal/build"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/types"
"go.k6.io/k6/metrics"
)
Expand Down Expand Up @@ -37,7 +37,7 @@ func optionFlagSet() *pflag.FlagSet {
flags.Int64("batch", 20, "max parallel batch reqs")
flags.Int64("batch-per-host", 6, "max parallel batch reqs per host")
flags.Int64("rps", 0, "limit requests per second")
flags.String("user-agent", fmt.Sprintf("k6/%s (https://k6.io/)", consts.Version), "user agent for http requests")
flags.String("user-agent", fmt.Sprintf("k6/%s (https://k6.io/)", build.Version), "user agent for http requests")
flags.String("http-debug", "", "log all HTTP requests and responses. Excludes body by default. To include body use '--http-debug=full'") //nolint:lll
flags.Lookup("http-debug").NoOptDefVal = "headers"
flags.Bool("insecure-skip-tls-verify", false, "skip verification of TLS certificates")
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/outputs_cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/metrics"
)

Expand Down Expand Up @@ -116,7 +116,7 @@ func createCloudTest(gs *state.GlobalState, test *loadedAndConfiguredTest) error
logger := gs.Logger.WithFields(logrus.Fields{"output": builtinOutputCloud.String()})

apiClient := cloudapi.NewClient(
logger, conf.Token.String, conf.Host.String, consts.Version, conf.Timeout.TimeDuration())
logger, conf.Token.String, conf.Host.String, build.Version, conf.Timeout.TimeDuration())

response, err := apiClient.CreateTestRun(testRun)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ import (
"runtime"
"strconv"

"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/execution"
"go.k6.io/k6/internal/usage"
"go.k6.io/k6/lib/consts"
)

func createReport(u *usage.Usage, execScheduler *execution.Scheduler) map[string]any {
execState := execScheduler.GetState()
m := u.Map()

m["k6_version"] = consts.Version
m["k6_version"] = build.Version
m["duration"] = execState.GetCurrentTestRunDuration().String()
m["goos"] = runtime.GOOS
m["goarch"] = runtime.GOARCH
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/report_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/execution"
"go.k6.io/k6/internal/execution/local"
"go.k6.io/k6/internal/lib/testutils"
"go.k6.io/k6/internal/usage"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/executor"
"gopkg.in/guregu/null.v3"
)
Expand Down Expand Up @@ -53,7 +53,7 @@ func TestCreateReport(t *testing.T) {
m := createReport(usage.New(), s)
require.NoError(t, err)

assert.Equal(t, consts.Version, m["k6_version"])
assert.Equal(t, build.Version, m["k6_version"])
assert.EqualValues(t, map[string]int{"shared-iterations": 1}, m["executors"])
assert.EqualValues(t, 6, m["vus_max"])
assert.EqualValues(t, 170, m["iterations"])
Expand All @@ -75,7 +75,7 @@ func TestCreateReport(t *testing.T) {
m := createReport(usage.New(), s)
require.NoError(t, err)

assert.Equal(t, consts.Version, m["k6_version"])
assert.Equal(t, build.Version, m["k6_version"])
assert.EqualValues(t, map[string]int{"shared-iterations": 1}, m["executors"])
assert.EqualValues(t, 0, m["vus_max"])
assert.EqualValues(t, 0, m["iterations"])
Expand All @@ -97,7 +97,7 @@ func TestCreateReport(t *testing.T) {
m := createReport(usage.New(), s)
require.NoError(t, err)

assert.Equal(t, consts.Version, m["k6_version"])
assert.Equal(t, build.Version, m["k6_version"])
assert.EqualValues(t, map[string]int{"shared-iterations": 1}, m["executors"])
assert.EqualValues(t, 0, m["vus_max"])
assert.EqualValues(t, 0, m["iterations"])
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/tests/cmd_run_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ import (
"github.com/tidwall/gjson"
"go.k6.io/k6/cloudapi"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/cmd"
"go.k6.io/k6/internal/cmd/tests/events"
"go.k6.io/k6/internal/event"
"go.k6.io/k6/internal/lib/testutils"
"go.k6.io/k6/internal/lib/testutils/httpmultibin"
"go.k6.io/k6/js/modules"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/lib/fsext"
)

Expand All @@ -52,7 +52,7 @@ func TestVersion(t *testing.T) {
cmd.ExecuteWithGlobalState(ts.GlobalState)

stdout := ts.Stdout.String()
assert.Contains(t, stdout, "k6 v"+consts.Version)
assert.Contains(t, stdout, "k6 v"+build.Version)
assert.Contains(t, stdout, runtime.Version())
assert.Contains(t, stdout, runtime.GOOS)
assert.Contains(t, stdout, runtime.GOARCH)
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/ui.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ import (

"go.k6.io/k6/cmd/state"
"go.k6.io/k6/internal/metrics/engine"
"go.k6.io/k6/internal/ui"
"go.k6.io/k6/internal/ui/pb"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/output"
)

Expand Down Expand Up @@ -56,7 +56,7 @@ func getBanner(noColor bool, isTrueColor bool) string {
c = color.RGB(0xFF, 0x67, 0x1d).Add(color.Bold)
}
c = setColor(noColor, c)
return c.Sprint(consts.Banner())
return c.Sprint(ui.Banner())
}

// isTrueColor returns true if the terminal supports true color (24-bit color).
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/spf13/cobra"
"go.k6.io/k6/cmd/state"
"go.k6.io/k6/ext"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/internal/build"
)

const (
Expand Down Expand Up @@ -45,7 +45,7 @@ func fullVersion() string {

// versionDetails returns the structured details about version
func versionDetails() map[string]interface{} {
v := consts.Version
v := build.Version
if !strings.HasPrefix(v, "v") {
v = "v" + v
}
Expand Down
8 changes: 4 additions & 4 deletions internal/cmd/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/cmd/tests"
"go.k6.io/k6/lib/consts"
)

func TestVersionFlag(t *testing.T) {
Expand All @@ -25,7 +25,7 @@ func TestVersionFlag(t *testing.T) {

// Check that the version/format string is correct
assert.Contains(t, stdout, "k6 v")
assert.Contains(t, stdout, consts.Version)
assert.Contains(t, stdout, build.Version)
assert.Contains(t, stdout, runtime.Version())
assert.Contains(t, stdout, runtime.GOOS)
assert.Contains(t, stdout, runtime.GOARCH)
Expand All @@ -46,7 +46,7 @@ func TestVersionSubCommand(t *testing.T) {

// Check that the version/format string is correct
assert.Contains(t, stdout, "k6 v")
assert.Contains(t, stdout, consts.Version)
assert.Contains(t, stdout, build.Version)
assert.Contains(t, stdout, runtime.Version())
assert.Contains(t, stdout, runtime.GOOS)
assert.Contains(t, stdout, runtime.GOARCH)
Expand Down Expand Up @@ -75,7 +75,7 @@ func TestVersionJSONSubCommand(t *testing.T) {
assert.Contains(t, details, "go_version")
assert.Contains(t, details, "go_os")
assert.Contains(t, details, "go_arch")
assert.Equal(t, "v"+consts.Version, details["version"])
assert.Equal(t, "v"+build.Version, details["version"])
assert.Equal(t, runtime.Version(), details["go_version"])
assert.Equal(t, runtime.GOOS, details["go_os"])
assert.Equal(t, runtime.GOARCH, details["go_arch"])
Expand Down
3 changes: 2 additions & 1 deletion internal/js/bundle.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/event"
"go.k6.io/k6/internal/js/compiler"
"go.k6.io/k6/internal/js/eventloop"
Expand Down Expand Up @@ -178,7 +179,7 @@ func (b *Bundle) makeArchive() *lib.Archive {
PwdURL: clonedPwdURL,
Env: make(map[string]string, len(b.preInitState.RuntimeOptions.Env)),
CompatibilityMode: b.CompatibilityMode.String(),
K6Version: consts.Version,
K6Version: build.Version,
Goos: runtime.GOOS,
}
// Copy env so changes in the archive are not reflected in the source Bundle
Expand Down
5 changes: 3 additions & 2 deletions internal/js/bundle_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/stretchr/testify/require"
"gopkg.in/guregu/null.v3"

"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/lib/testutils"
"go.k6.io/k6/internal/loader"
"go.k6.io/k6/internal/usage"
Expand Down Expand Up @@ -584,7 +585,7 @@ func TestNewBundleFromArchive(t *testing.T) {
arc := &lib.Archive{
Type: "js",
FilenameURL: &url.URL{Scheme: "file", Path: "/script"},
K6Version: consts.Version,
K6Version: build.Version,
Data: []byte(code),
Options: lib.Options{VUs: null.IntFrom(999)},
PwdURL: &url.URL{Scheme: "file", Path: "/"},
Expand Down Expand Up @@ -948,7 +949,7 @@ func TestBundleMakeArchive(t *testing.T) {
fileData, err := fsext.ReadFile(arc.Filesystems["file"], "/path/to/file.txt")
require.NoError(t, err)
assert.Equal(t, `hi`, string(fileData))
assert.Equal(t, consts.Version, arc.K6Version)
assert.Equal(t, build.Version, arc.K6Version)
assert.Equal(t, tc.cm.String(), arc.CompatibilityMode)
})
}
Expand Down
4 changes: 2 additions & 2 deletions internal/output/cloud/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import (

"go.k6.io/k6/cloudapi"
"go.k6.io/k6/errext"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/internal/usage"
"go.k6.io/k6/lib"
"go.k6.io/k6/lib/consts"
"go.k6.io/k6/metrics"
"go.k6.io/k6/output"
cloudv2 "go.k6.io/k6/output/cloud/expv2"
Expand Down Expand Up @@ -141,7 +141,7 @@ func newOutput(params output.Params) (*Output, error) {
}

apiClient := cloudapi.NewClient(
logger, conf.Token.String, conf.Host.String, consts.Version, conf.Timeout.TimeDuration())
logger, conf.Token.String, conf.Host.String, build.Version, conf.Timeout.TimeDuration())

return &Output{
config: conf,
Expand Down
4 changes: 2 additions & 2 deletions internal/output/opentelemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/mstoykov/envconfig"
"go.k6.io/k6/errext"
"go.k6.io/k6/errext/exitcodes"
k6Const "go.k6.io/k6/lib/consts"
"go.k6.io/k6/internal/build"
"go.k6.io/k6/lib/types"
"gopkg.in/guregu/null.v3"
)
Expand Down Expand Up @@ -106,7 +106,7 @@ func GetConsolidatedConfig(jsonRawConf json.RawMessage, env map[string]string) (
func newDefaultConfig() Config {
return Config{
ServiceName: null.StringFrom("k6"),
ServiceVersion: null.StringFrom(k6Const.Version),
ServiceVersion: null.StringFrom(build.Version),
ExporterType: null.StringFrom(grpcExporterType),

HTTPExporterInsecure: null.BoolFrom(false),
Expand Down
Loading

0 comments on commit 19a4eae

Please sign in to comment.