Documentation
¶
Overview ¶
Package k6deps contains the data model of the k6 script dependencies and the operations related to them.
Index ¶
- Constants
- Variables
- type Dependencies
- func (deps Dependencies) MarshalJS() ([]byte, error)
- func (deps Dependencies) MarshalJSON() ([]byte, error)
- func (deps Dependencies) MarshalText() ([]byte, error)
- func (deps Dependencies) Merge(from Dependencies) error
- func (deps Dependencies) Sorted() []*Dependency
- func (deps Dependencies) String() string
- func (deps *Dependencies) UnmarshalJS(text []byte) error
- func (deps *Dependencies) UnmarshalJSON(data []byte) error
- func (deps *Dependencies) UnmarshalText(text []byte) error
- type Dependency
- type Options
- type Source
Examples ¶
Constants ¶
const ( // ConstraintsAny is a wildcard constraint that any version matches. ConstraintsAny = "*" // NameK6 is the name of the k6 dependency, its value is "k6". NameK6 = "k6" )
const EnvDependencies = "K6_DEPENDENCIES"
EnvDependencies holds the name of the environment variable thet describes additional dependencies.
Variables ¶
var ( ErrConstraints = errors.New("constraints error") ErrDependency = errors.New("dependency error") )
Functions ¶
This section is empty.
Types ¶
type Dependencies ¶
type Dependencies map[string]*Dependency
Dependencies contains the dependencies of the k6 test script in map format. The key of the map is the name of the dependency.
func Analyze ¶
func Analyze(opts *Options) (Dependencies, error)
Analyze searches, loads and analyzes the specified sources, extracting the k6 extensions and their version constraints. Note: if archive is specified, the other three sources will not be taken into account, since the archive may contain them.
Example (With_pragma) ¶
package main import ( "fmt" "github.com/grafana/k6deps" ) const scriptWithPragma = ` "use k6 > 0.54"; "use k6 with k6/x/faker > 0.4.0"; "use k6 with k6/x/sql >= 1.0.1"; import { Faker } from "k6/x/faker"; import sql from "k6/x/sql"; import driver from "k6/x/sql/driver/ramsql"; export default function() { } ` func main() { deps, _ := k6deps.Analyze(&k6deps.Options{ Script: k6deps.Source{ Name: "script.js", Contents: []byte(scriptWithPragma), }, // disable automatic source detection Manifest: k6deps.Source{Ignore: true}, Env: k6deps.Source{Ignore: true}, }) fmt.Println(deps.String()) out, _ := deps.MarshalJSON() fmt.Println(string(out)) }
Output: k6>0.54;k6/x/faker>0.4.0;k6/x/sql>=1.0.1;k6/x/sql/driver/ramsql* {"k6":">0.54","k6/x/faker":">0.4.0","k6/x/sql":">=1.0.1","k6/x/sql/driver/ramsql":"*"}
Example (Without_pragma) ¶
package main import ( "fmt" "github.com/grafana/k6deps" ) const scriptWithoutPragma = ` import { Faker } from "k6/x/faker"; import sql from "k6/x/sql"; import driver from "k6/x/sql/driver/ramsql"; export default function() { } ` func main() { deps, _ := k6deps.Analyze(&k6deps.Options{ Script: k6deps.Source{ Name: "script.js", Contents: []byte(scriptWithoutPragma), }, // disable automatic source detection Manifest: k6deps.Source{Ignore: true}, Env: k6deps.Source{Ignore: true}, }) fmt.Println(deps.String()) out, _ := deps.MarshalJSON() fmt.Println(string(out)) }
Output: k6/x/faker*;k6/x/sql*;k6/x/sql/driver/ramsql* {"k6/x/faker":"*","k6/x/sql":"*","k6/x/sql/driver/ramsql":"*"}
func (Dependencies) MarshalJS ¶
func (deps Dependencies) MarshalJS() ([]byte, error)
MarshalJS marshals dependencies into a consecutive, one-line JavaScript string directive format. The first element of the series is "k6" (if there is one), the following elements follow each other in lexically increasing order based on the name.
func (Dependencies) MarshalJSON ¶
func (deps Dependencies) MarshalJSON() ([]byte, error)
MarshalJSON marshals dependencies into JSON object format. The property names will be the dependency names, and the property values will be the text format used by MarshalText of the given dependency.
func (Dependencies) MarshalText ¶
func (deps Dependencies) MarshalText() ([]byte, error)
MarshalText marshals the dependencies into a single-line text format. The text format is a semicolon-separated sequence of the text format of each dependency. The first element of the series is "k6" (if there is one), the following elements follow each other in lexically increasing order based on the name. For example: k6>0.49;k6/x/faker>=0.2.0;k6/x/toml>v0.1.0;xk6-dashboard*
func (Dependencies) Merge ¶
func (deps Dependencies) Merge(from Dependencies) error
Merge updates deps dependencies based on from dependencies. Adds a dependency that doesn't exist yet. If the dependency exists in both collections, but one of them does not have version constraints, then the dependency with version constraints is placed in deps. Otherwise, i.e. if the dependency is included in both collections and in both with version constraints, an error is generated.
func (Dependencies) Sorted ¶ added in v0.1.2
func (deps Dependencies) Sorted() []*Dependency
Sorted returns dependencies as an array, with "k6" as a first element (if any) and the rest of the array is sorted by name lexicographically.
func (Dependencies) String ¶
func (deps Dependencies) String() string
String converts the dependencies to displayable text format. The format is the same as that used by MarshalText.
func (*Dependencies) UnmarshalJS ¶
func (deps *Dependencies) UnmarshalJS(text []byte) error
UnmarshalJS unmarshals dependencies from a series of string directives in the format used by MarshalJS.
func (*Dependencies) UnmarshalJSON ¶
func (deps *Dependencies) UnmarshalJSON(data []byte) error
UnmarshalJSON unmarshals dependencies from a JSON object in the format used by MarshalJSON.
func (*Dependencies) UnmarshalText ¶
func (deps *Dependencies) UnmarshalText(text []byte) error
UnmarshalText parses the one-line text dependencies format into the *deps variable.
type Dependency ¶
type Dependency struct { // Name is the name of the dependency. Name string `json:"name,omitempty"` // Constraints contains the version constraints of the dependency. Constraints *semver.Constraints `json:"constraints,omitempty"` }
Dependency contains the properties of a k6 dependency (extension or k6 core).
func NewDependency ¶
func NewDependency(name, constraints string) (*Dependency, error)
NewDependency creates a new Dependency instance with the given name. If the constraints parameter is not empty, it will be parsed as version constraints.
func (*Dependency) GetConstraints ¶ added in v0.1.2
func (dep *Dependency) GetConstraints() *semver.Constraints
GetConstraints returns Constraints or the default constraints ("*") if Constraints is nil
func (*Dependency) MarshalJS ¶
func (dep *Dependency) MarshalJS() ([]byte, error)
MarshalJS marshals the dependency into a one-line JavaScript string directive format. For example: "us k6 with k6/x/faker>0.1.0";
func (*Dependency) MarshalText ¶
func (dep *Dependency) MarshalText() ([]byte, error)
MarshalText marshals the dependency into a single-line text format. For example: k6/x/faker>0.1.0
func (*Dependency) String ¶
func (dep *Dependency) String() string
String converts the dependency to displayable text format. The format is the same as that used by MarshalText.
func (*Dependency) UnmarshalText ¶
func (dep *Dependency) UnmarshalText(text []byte) error
UnmarshalText parses the one-line text dependency format into the *dep variable.
type Options ¶
type Options struct { // Script contains the properties of the k6 test script to be analyzed. Script Source // Archive contains the properties of the k6 archive to be analyzed. // If archive is specified, the other three sources will not be taken into account, // since the archive may contain them. // An archive is a tar file, which can be created using the k6 archive command, for example. Archive Source // Manifest contains the properties of the manifest file to be analyzed. // If the Ignore property is not set and no manifest file is specified, // the package.json file closest to the script is searched for. Manifest Source // Env contains the properties of the environment variable to be analyzed. // If the Ignore property is not set and no variable is specified, // the value of the variable named K6_DEPENDENCIES is read. Env Source // LookupEnv function is used to query the value of the environment variable // specified in the Env option Name if the Contents of the Env option is empty. // If empty, os.LookupEnv will be used. LookupEnv func(key string) (value string, ok bool) // FindManifest function is used to find manifest file for the given scriptfile // if the Contents of Manifest option is empty. // If the scriptfile parameter is empty, FindManifest starts searching // for the manifest file from the current directory // If missing, the closest manifest file will be used. FindManifest func(scriptfile string) (contents []byte, filename string, ok bool, err error) }
Options contains the parameters of the dependency analysis.
type Source ¶
type Source struct { // Name contains the name of the source (file, environment variable, etc.). Name string // Reader provides streaming access to the source content as an alternative to Contents. Reader io.Reader // Contents contains the content of the source (e.g. script) Contents []byte // Ignore disables automatic search and processing of that source. Ignore bool }
Source describes a generic dependency source. Such a source can be the k6 script, the manifest file, or an environment variable (e.g. K6_DEPENDENCIES).
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package cmd contains k6deps cobra command factory function.
|
Package cmd contains k6deps cobra command factory function. |
k6deps
Package main contains the main function for k6deps CLI tool.
|
Package main contains the main function for k6deps CLI tool. |
tools
|
|
gendoc
Package main contains CLI documentation generator tool.
|
Package main contains CLI documentation generator tool. |