-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Add deps command to return dependencies of a given script/archive #5410
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
Open
mstoykov
wants to merge
5
commits into
master
Choose a base branch
from
k6-deps-command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+323
−42
Open
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3160bde
Add deps command to return dependancies of a given script/archive
mstoykov 80f5f16
Rename dependancies to buildDependancies
mstoykov 2de24c6
Update internal/cmd/deps.go
mstoykov b5e955a
typos
mstoykov 2bf1c66
Merge branch 'master' into k6-deps-command
mstoykov 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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/json" | ||
| "errors" | ||
| "slices" | ||
|
|
||
| "github.com/spf13/cobra" | ||
|
|
||
| "go.k6.io/k6/cmd/state" | ||
| "go.k6.io/k6/ext" | ||
| "go.k6.io/k6/internal/build" | ||
| ) | ||
|
|
||
| func getCmdDeps(gs *state.GlobalState) *cobra.Command { | ||
| depsCmd := &cobra.Command{ | ||
| Use: "deps", | ||
| Short: "Resolve dependencies of a test", | ||
| Long: `Resolve dependencies of a test including automatic extenstion resolution.` + | ||
| `And outputs all dependencies for the test and whether a custom build is required.`, | ||
| Args: cobra.ExactArgs(1), | ||
| RunE: func(cmd *cobra.Command, args []string) error { | ||
| test, err := loadLocalTestWithoutRunner(gs, cmd, args) | ||
| if err != nil { | ||
| var unsatisfiedErr binaryIsNotSatisfyingDependenciesError | ||
| if !errors.As(err, &unsatisfiedErr) { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| deps := test.Dependencies() | ||
| depsMap := map[string]string{} | ||
| for name, constraint := range deps { | ||
| if constraint == nil { | ||
| depsMap[name] = "*" | ||
| continue | ||
| } | ||
| depsMap[name] = constraint.String() | ||
| } | ||
| imports := test.Imports() | ||
| slices.Sort(imports) | ||
|
|
||
| result := struct { | ||
| BuildDependancies map[string]string `json:"buildDependancies"` | ||
| Imports []string `json:"imports"` | ||
| CustomBuildRequired bool `json:"customBuildRequired"` | ||
| }{ | ||
| BuildDependancies: depsMap, | ||
| Imports: imports, | ||
| CustomBuildRequired: isCustomBuildRequired(deps, build.Version, ext.GetAll()), | ||
| } | ||
|
|
||
| buf := &bytes.Buffer{} | ||
| enc := json.NewEncoder(buf) | ||
| enc.SetEscapeHTML(false) | ||
| enc.SetIndent("", " ") | ||
| if err := enc.Encode(result); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| printToStdout(gs, buf.String()) | ||
| return nil | ||
| }, | ||
| } | ||
|
|
||
| depsCmd.Flags().SortFlags = false | ||
| depsCmd.Flags().AddFlagSet(runtimeOptionFlagSet(false)) | ||
|
|
||
| return depsCmd | ||
| } | ||
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,140 @@ | ||
| package cmd | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "slices" | ||
| "strings" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
|
|
||
| "go.k6.io/k6/internal/cmd/tests" | ||
| "go.k6.io/k6/internal/lib/testutils" | ||
| ) | ||
|
|
||
| func TestGetCmdDeps(t *testing.T) { | ||
| t.Parallel() | ||
| testCases := []struct { | ||
| name string | ||
| files map[string][]byte | ||
| expectedDeps map[string]string | ||
| expectCustomBuild bool | ||
| expectedImports []string | ||
| }{ | ||
| { | ||
| name: "single external dependency", | ||
| files: map[string][]byte{ | ||
| "/main.js": []byte(`import http from "k6/http"; | ||
| import foo from "k6/x/foo"; | ||
|
|
||
| export default function () { | ||
| http.get("https://example.com"); | ||
| foo(); | ||
| } | ||
| `), | ||
| }, | ||
| expectedDeps: map[string]string{"k6/x/foo": "*"}, | ||
| expectCustomBuild: true, | ||
| expectedImports: []string{"/main.js", "k6/http", "k6/x/foo"}, | ||
| }, | ||
| { | ||
| name: "no external dependency", | ||
| files: map[string][]byte{ | ||
| "/main.js": []byte(`import http from "k6/http"; | ||
|
|
||
| export default function () { | ||
| http.get("https://example.com"); | ||
| } | ||
| `), | ||
| }, | ||
| expectedDeps: map[string]string{}, | ||
| expectCustomBuild: false, | ||
| expectedImports: []string{"/main.js", "k6/http"}, | ||
| }, | ||
| { | ||
| name: "nested local imports", | ||
| files: map[string][]byte{ | ||
| "/main.js": []byte(`import helper from "./lib/helper.js"; | ||
|
|
||
| export default function () { | ||
| helper(); | ||
| } | ||
| `), | ||
| "/lib/helper.js": []byte(`import nested from "../shared/nested.js"; | ||
| import ext from "k6/x/bar"; | ||
|
|
||
| export default function () { | ||
| nested(); | ||
| ext(); | ||
| } | ||
| `), | ||
| "/shared/nested.js": []byte(`export default function () { | ||
| return "nested"; | ||
| } | ||
| `), | ||
| }, | ||
| expectedDeps: map[string]string{"k6/x/bar": "*"}, | ||
| expectCustomBuild: true, | ||
| expectedImports: []string{"/lib/helper.js", "/main.js", "/shared/nested.js", "k6/x/bar"}, | ||
| }, | ||
| { | ||
| name: "use directive across files", | ||
| files: map[string][]byte{ | ||
| "/main.js": []byte(`import directive from "./modules/with-directive.js"; | ||
|
|
||
| export default function () { | ||
| directive(); | ||
| } | ||
| `), | ||
| "/modules/with-directive.js": []byte(`"use k6 with k6/x/alpha >= 1.2.3"; | ||
| import beta from "k6/x/beta"; | ||
| import util from "./util.js"; | ||
|
|
||
| export default function () { | ||
| util(); | ||
| beta(); | ||
| } | ||
| `), | ||
| "/modules/util.js": []byte(`export default function () { | ||
| return "util"; | ||
| } | ||
| `), | ||
| }, | ||
| expectedDeps: map[string]string{"k6/x/alpha": ">=1.2.3", "k6/x/beta": "*"}, | ||
| expectCustomBuild: true, | ||
| expectedImports: []string{"/main.js", "/modules/util.js", "/modules/with-directive.js", "k6/x/beta"}, | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| t.Parallel() | ||
| ts := tests.NewGlobalTestState(t) | ||
| ts.FS = testutils.MakeMemMapFs(t, tc.files) | ||
|
|
||
| cmd := getCmdDeps(ts.GlobalState) | ||
| cmd.SetArgs([]string{"/main.js"}) | ||
| require.NoError(t, cmd.Execute()) | ||
|
|
||
| var output struct { | ||
| BuildDependancies map[string]string `json:"buildDependancies"` | ||
| Imports []string `json:"imports"` | ||
| CustomBuildRequired bool `json:"customBuildRequired"` | ||
| } | ||
| require.NoError(t, json.Unmarshal(ts.Stdout.Bytes(), &output)) | ||
|
|
||
| require.Equal(t, tc.expectedDeps, output.BuildDependancies) | ||
|
|
||
| require.Equal(t, tc.expectCustomBuild, output.CustomBuildRequired) | ||
|
|
||
| expectedImports := slices.Clone(tc.expectedImports) | ||
| for i, expectedImport := range tc.expectedImports { | ||
| if !strings.HasPrefix(expectedImport, "k6") { | ||
| expectedImports[i] = "file://" + expectedImport | ||
| } | ||
| } | ||
|
|
||
| require.EqualValues(t, expectedImports, output.Imports) | ||
| }) | ||
| } | ||
| } |
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the expected behaviour when the error is a
binaryIsNotSatisfyingDependenciesError? Looks like we're just ignoring it. Maybe we should log a debug log if we can ignore it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here the idea is that if this is the error - this means to run this we need a new binary with som extesnions.
But as we only care about the dependancies and that is already what we've got, here we just check that if this isn't the error, as in that case we need to return it.