state

package
v0.55.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2024 License: AGPL-3.0 Imports: 13 Imported by: 6

Documentation

Overview

Package state contains the types and functionality used for keeping track of cmd-related values that are used globally throughout k6. It also exposes some related test types and helpers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildEnvMap

func BuildEnvMap(environ []string) map[string]string

BuildEnvMap returns a map from raw environment values, such as returned from os.Environ().

func ParseEnvKeyValue

func ParseEnvKeyValue(kv string) (string, string)

ParseEnvKeyValue splits an environment variable string into key and value.

Types

type GlobalFlags

type GlobalFlags struct {
	ConfigFilePath   string
	Quiet            bool
	NoColor          bool
	Address          string
	ProfilingEnabled bool
	LogOutput        string
	LogFormat        string
	Verbose          bool
}

GlobalFlags contains global config values that apply for all k6 sub-commands.

func GetDefaultFlags

func GetDefaultFlags(homeDir string) GlobalFlags

GetDefaultFlags returns the default global flags.

type GlobalState

type GlobalState struct {
	Ctx context.Context

	FS         fsext.Fs
	Getwd      func() (string, error)
	BinaryName string
	CmdArgs    []string
	Env        map[string]string
	Events     *event.System

	DefaultFlags, Flags GlobalFlags

	OutMutex       *sync.Mutex
	Stdout, Stderr *console.Writer
	Stdin          io.Reader

	OSExit       func(int)
	SignalNotify func(chan<- os.Signal, ...os.Signal)
	SignalStop   func(chan<- os.Signal)

	Logger         *logrus.Logger //nolint:forbidigo //TODO:change to FieldLogger
	FallbackLogger logrus.FieldLogger
}

GlobalState contains the GlobalFlags and accessors for most of the global process-external state like CLI arguments, env vars, standard input, output and error, etc. In practice, most of it is normally accessed through the `os` package from the Go stdlib.

We group them here so we can prevent direct access to them from the rest of the k6 codebase. This gives us the ability to mock them and have robust and easy-to-write integration-like tests to check the k6 end-to-end behavior in any simulated conditions.

`NewGlobalState()` returns a globalState object with the real `os` parameters, while `NewGlobalTestState()` can be used in tests to create simulated environments.

func NewGlobalState

func NewGlobalState(ctx context.Context) *GlobalState

NewGlobalState returns a new GlobalState with the given ctx. Ideally, this should be the only function in the whole codebase where we use global variables and functions from the os package. Anywhere else, things like os.Stdout, os.Stderr, os.Stdin, os.Getenv(), etc. should be removed and the respective properties of globalState used instead.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL