Skip to content

Commit

Permalink
Improve some code identifier names and comments
Browse files Browse the repository at this point in the history
  • Loading branch information
na-- committed Mar 16, 2022
1 parent 1455a62 commit 6ffdb95
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 18 deletions.
2 changes: 1 addition & 1 deletion cmd/archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ An archive is a fully self-contained test run, and can be executed identically e
k6 run myarchive.tar`[1:],
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
test, err := loadTest(gs, cmd, args, getPartialConfig)
test, err := loadAndConfigureTest(gs, cmd, args, getPartialConfig)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
)
printBar(globalState, progressBar)

test, err := loadTest(globalState, cmd, args, getPartialConfig)
test, err := loadAndConfigureTest(globalState, cmd, args, getPartialConfig)
if err != nil {
return err
}
Expand Down Expand Up @@ -166,7 +166,7 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth

name := cloudConfig.Name.String
if !cloudConfig.Name.Valid || cloudConfig.Name.String == "" {
name = filepath.Base(test.testPath)
name = filepath.Base(test.sourceRootPath)
}

globalCtx, globalCancel := context.WithCancel(globalState.ctx)
Expand Down Expand Up @@ -216,7 +216,7 @@ This will execute the test on the k6 cloud service. Use "k6 login cloud" to auth
testURL := cloudapi.URLForResults(refID, cloudConfig)
executionPlan := test.derivedConfig.Scenarios.GetFullExecutionRequirements(et)
printExecutionDescription(
globalState, "cloud", test.testPath, testURL, test.derivedConfig, et, executionPlan, nil,
globalState, "cloud", test.sourceRootPath, testURL, test.derivedConfig, et, executionPlan, nil,
)

modifyAndPrintBar(
Expand Down
2 changes: 1 addition & 1 deletion cmd/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func getInspectCmd(gs *globalState) *cobra.Command {
Long: `Inspect a script or archive.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
test, err := loadTest(gs, cmd, args, nil)
test, err := loadAndConfigureTest(gs, cmd, args, nil)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ a commandline interface for interacting with it.`,
RunE: func(cmd *cobra.Command, args []string) error {
printBanner(globalState)

test, err := loadTest(globalState, cmd, args, getConfig)
test, err := loadAndConfigureTest(globalState, cmd, args, getConfig)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/runtime_options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func testRuntimeOptionsCase(t *testing.T, tc runtimeOptionsTestCase) {
ts := newGlobalTestState(t) // TODO: move upwards, make this into an almost full integration test
registry := metrics.NewRegistry()
test := &loadedTest{
testPath: "script.js",
sourceRootPath: "script.js",
source: &loader.SourceData{Data: jsCode.Bytes(), URL: &url.URL{Path: "/script.js", Scheme: "file"}},
fileSystems: map[string]afero.Fs{"file": fs},
runtimeOptions: rtOpts,
Expand All @@ -97,7 +97,7 @@ func testRuntimeOptionsCase(t *testing.T, tc runtimeOptionsTestCase) {

getRunnerErr := func(rtOpts lib.RuntimeOptions) *loadedTest {
return &loadedTest{
testPath: "script.tar",
sourceRootPath: "script.tar",
source: &loader.SourceData{Data: archiveBuf.Bytes(), URL: &url.URL{Path: "/script.tar", Scheme: "file"}},
fileSystems: map[string]afero.Fs{"file": fs},
runtimeOptions: rtOpts,
Expand Down
25 changes: 15 additions & 10 deletions cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,24 @@ const (
testTypeArchive = "archive"
)

// loadedTest contains all of data, details and dependencies of a fully-loaded
// and configured k6 test.
type loadedTest struct {
testPath string // contains the raw string the user supplied
sourceRootPath string // contains the raw string the user supplied
source *loader.SourceData
fileSystems map[string]afero.Fs
runtimeOptions lib.RuntimeOptions
metricsRegistry *metrics.Registry
builtInMetrics *metrics.BuiltinMetrics
initRunner lib.Runner // TODO: rename to something more appropriate

// Only set if cliConfigGetter is supplied to loadTest() or if
// Only set if cliConfigGetter is supplied to loadAndConfigureTest() or if
// consolidateDeriveAndValidateConfig() is manually called.
consolidatedConfig Config
derivedConfig Config
}

func loadTest(
func loadAndConfigureTest(
gs *globalState, cmd *cobra.Command, args []string,
// supply this if you want the test config consolidated and validated
cliConfigGetter func(flags *pflag.FlagSet) (Config, error), // TODO: obviate
Expand All @@ -45,14 +47,17 @@ func loadTest(
return nil, fmt.Errorf("k6 needs at least one argument to load the test")
}

testPath := args[0]
gs.logger.Debugf("Resolving and reading test '%s'...", testPath)
src, fileSystems, err := readSource(gs, testPath)
sourceRootPath := args[0]
gs.logger.Debugf("Resolving and reading test '%s'...", sourceRootPath)
src, fileSystems, err := readSource(gs, sourceRootPath)
if err != nil {
return nil, err
}
resolvedPath := src.URL.String()
gs.logger.Debugf("'%s' resolved to '%s' and successfully loaded %d bytes!", testPath, resolvedPath, len(src.Data))
gs.logger.Debugf(
"'%s' resolved to '%s' and successfully loaded %d bytes!",
sourceRootPath, resolvedPath, len(src.Data),
)

gs.logger.Debugf("Gathering k6 runtime options...")
runtimeOptions, err := getRuntimeOptions(cmd.Flags(), gs.envVars)
Expand All @@ -62,17 +67,17 @@ func loadTest(

registry := metrics.NewRegistry()
test := &loadedTest{
testPath: testPath,
sourceRootPath: sourceRootPath,
source: src,
fileSystems: fileSystems,
runtimeOptions: runtimeOptions,
metricsRegistry: registry,
builtInMetrics: metrics.RegisterBuiltinMetrics(registry),
}

gs.logger.Debugf("Initializing k6 runner for '%s' (%s)...", testPath, resolvedPath)
gs.logger.Debugf("Initializing k6 runner for '%s' (%s)...", sourceRootPath, resolvedPath)
if err := test.initializeFirstRunner(gs); err != nil {
return nil, fmt.Errorf("could not initialize '%s': %w", testPath, err)
return nil, fmt.Errorf("could not initialize '%s': %w", sourceRootPath, err)
}
gs.logger.Debug("Runner successfully initialized!")

Expand Down

0 comments on commit 6ffdb95

Please sign in to comment.