test

package
v8.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2023 License: MIT Imports: 29 Imported by: 0

Documentation

Overview

Package test provides the infrastructure for the end-to-end tests. End-to-end tests are written in Cucumber for Go (https://github.com/cucumber/godog). The main file running the tests is main_test.go. Each test scenario gets a fully working local Git repository. A good starting point for learning how the end-to-end test framework provisions these repository is GitManager.

Package envvars provides helper functions to work with lists of environment variables as provided by `os.Environ`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CallScriptArgs

func CallScriptArgs(toolPath string) (cmd string, args []string)

CallScriptArgs provides the command and arguments to call the given script on Windows.

func CopyDirectory

func CopyDirectory(src, dst string) error

CopyDirectory copies all files in the given src directory into the given dst directory. Both the source and the destination directory must exist.

func CreateInputTool

func CreateInputTool(toolPath string) error

CreateInputTool creates a tool that reads two inputs from STDIN and prints them back to the user.

func CreateLsTool

func CreateLsTool(toolPath string) error

CreateLsTool creates a tool in the given folder that lists all files in its current folder.

func DefaultCommit

func DefaultCommit(filenameSuffix string) git.Commit

DefaultCommit provides a new Commit instance populated with the default values used in the absence of value specified by the test.

func FromGherkinTable

func FromGherkinTable(table *messages.PickleStepArgument_PickleTable) ([]git.Commit, error)

FromGherkinTable provides a Commit collection representing the data in the given Gherkin table.

func PrependEnvPath

func PrependEnvPath(envVars []string, directory string) []string

PrependPath provides a new envvars with the given directory appended to the PATH entry of the given envvars. This function assumes there is only one PATH entry.

func RenderTable

func RenderTable(table *messages.PickleStepArgument_PickleTable) string

RenderTable provides the textual Gherkin representation of the given Gherkin table.

func ReplaceEnvVar

func ReplaceEnvVar(envVars []string, key string, value string) []string

Replace provides a new envvars in which the entry with the given key contains the given value instead of its original value. If no entry with the given key exists, appends one at the end. This function assumes that keys are unique, i.e. no duplicate keys exist.

func ScriptName

func ScriptName(command string) string

ScriptName provides the name of the given script file on the Windows.

func Steps

func Steps(suite *godog.Suite, state *ScenarioState)

Steps defines Cucumber step implementations around Git workspace management.

Types

type CommitTableBuilder

type CommitTableBuilder struct {
	// contains filtered or unexported fields
}

CommitTableBuilder collects data about commits in Git repositories in the same way that our Gherkin tables describing commits in repos are organized.

func NewCommitTableBuilder

func NewCommitTableBuilder() CommitTableBuilder

NewCommitTableBuilder provides a fully initialized instance of CommitTableBuilder.

func (*CommitTableBuilder) Add

func (builder *CommitTableBuilder) Add(commit git.Commit, location string)

Add registers the given commit from the given location into this table.

func (*CommitTableBuilder) AddMany

func (builder *CommitTableBuilder) AddMany(commits []git.Commit, location string)

AddMany registers the given commits from the given location into this table.

func (*CommitTableBuilder) Table

func (builder *CommitTableBuilder) Table(fields []string) DataTable

Table provides the data accumulated by this CommitTableBuilder as a DataTable.

type DataTable

type DataTable struct {
	// table data organized as rows and columns
	Cells [][]string `exhaustruct:"optional"`
}

DataTable allows comparing user-generated data with Gherkin tables. The zero value is an empty DataTable.

func FromGherkin

func FromGherkin(table *messages.PickleStepArgument_PickleTable) DataTable

FromGherkin provides a DataTable instance populated with data from the given Gherkin table.

func RenderExecutedGitCommands

func RenderExecutedGitCommands(commands []ExecutedGitCommand, table *messages.PickleStepArgument_PickleTable) DataTable

RenderExecutedGitCommands provides the textual Gherkin table representation of the given executed Git commands. The DataTable table matches the structure of the given Gherkin table.

func (*DataTable) AddRow

func (table *DataTable) AddRow(elements ...string)

AddRow adds the given row of table data to this table.

func (*DataTable) EqualDataTable

func (table *DataTable) EqualDataTable(other DataTable) (diff string, errorCount int)

EqualDataTable compares this DataTable instance to the given DataTable. If both are equal it returns an empty string, otherwise a diff printable on the console.

func (*DataTable) EqualGherkin

func (table *DataTable) EqualGherkin(other *messages.PickleStepArgument_PickleTable) (diff string, errorCount int)

EqualGherkin compares this DataTable instance to the given Gherkin table. If both are equal it returns an empty string, otherwise a diff printable on the console.

func (*DataTable) Expand

func (table *DataTable) Expand(localRepo *Runner, remoteRepo *Runner) (DataTable, error)

Expand returns a new DataTable instance with the placeholders in this datatable replaced with the given values.

func (*DataTable) RemoveText

func (table *DataTable) RemoveText(text string)

RemoveText deletes the given text from each cell.

func (*DataTable) Sort

func (table *DataTable) Sort()

Sorted provides a new DataTable that contains the content of this DataTable sorted by the first column.

func (*DataTable) String

func (table *DataTable) String() string

String provides the data in this DataTable instance formatted in Gherkin table format.

type ExecutedGitCommand

type ExecutedGitCommand struct {
	// Branch contains the branch in which this command ran.
	Branch string

	// Command contains the command executed.
	Command string
}

ExecutedGitCommand describes a Git command that was executed by Git Town during testing.

func GitCommandsInGitTownOutput

func GitCommandsInGitTownOutput(output string) []ExecutedGitCommand

GitCommandsInGitTownOutput provides the Git commands mentioned in the given Git Town output.

type GitEnvironment

type GitEnvironment struct {
	// Dir defines the local folder in which this GitEnvironment is stored.
	// This folder also acts as the HOME directory for tests using this GitEnvironment.
	// It contains the global Git configuration to use in this test.
	Dir string

	// CoworkerRepo is the optional Git repository that is locally checked out at the coworker machine.
	CoworkerRepo *Runner `exhaustruct:"optional"`

	// DevRepo is the Git repository that is locally checked out at the developer machine.
	DevRepo Runner `exhaustruct:"optional"`

	// OriginRepo is the Git repository that simulates the origin repo (on GitHub).
	// If this value is nil, the current test setup has no origin.
	OriginRepo *Runner `exhaustruct:"optional"`

	// SubmoduleRepo is the Git repository that simulates an external repo used as a submodule.
	// If this value is nil, the current test setup uses no submodules.
	SubmoduleRepo *Runner `exhaustruct:"optional"`

	// UpstreamRepo is the optional Git repository that contains the upstream for this environment.
	UpstreamRepo *Runner `exhaustruct:"optional"`
}

GitEnvironment is a complete Git environment for a Cucumber scenario.

func CloneGitEnvironment

func CloneGitEnvironment(original GitEnvironment, dir string) (GitEnvironment, error)

CloneGitEnvironment provides a GitEnvironment instance in the given directory, containing a copy of the given GitEnvironment.

func NewStandardGitEnvironment

func NewStandardGitEnvironment(dir string) (GitEnvironment, error)

NewStandardGitEnvironment provides a GitEnvironment in the given directory, fully populated as a standardized setup for scenarios.

The origin repo has the initial branch checked out. Git repos cannot receive pushes of the currently checked out branch because that will change files in the current workspace. The tests don't use the initial branch.

func (*GitEnvironment) AddCoworkerRepo

func (env *GitEnvironment) AddCoworkerRepo() error

AddCoworkerRepo adds a coworker repository.

func (*GitEnvironment) AddSubmoduleRepo

func (env *GitEnvironment) AddSubmoduleRepo() error

AddSubmodule adds a submodule repository.

func (*GitEnvironment) AddUpstream

func (env *GitEnvironment) AddUpstream() error

AddUpstream adds an upstream repository.

func (*GitEnvironment) Branches

func (env *GitEnvironment) Branches() (DataTable, error)

Branches provides a tabular list of all branches in this GitEnvironment.

func (GitEnvironment) CommitTable

func (env GitEnvironment) CommitTable(fields []string) (DataTable, error)

CommitTable provides a table for all commits in this Git environment containing only the given fields.

func (*GitEnvironment) CreateCommits

func (env *GitEnvironment) CreateCommits(commits []git.Commit) error

CreateCommits creates the commits described by the given Gherkin table in this Git repository.

func (GitEnvironment) CreateOriginBranch

func (env GitEnvironment) CreateOriginBranch(name, parent string) error

CreateOriginBranch creates a branch with the given name only in the origin directory.

func (GitEnvironment) CreateTags

func (env GitEnvironment) CreateTags(table *messages.PickleStepArgument_PickleTable) error

CreateTags creates tags from the given gherkin table.

func (GitEnvironment) Remove

func (env GitEnvironment) Remove() error

Remove deletes all files used by this GitEnvironment from disk.

func (GitEnvironment) TagTable

func (env GitEnvironment) TagTable() (DataTable, error)

TagTable provides a table for all tags in this Git environment.

type GitManager

type GitManager struct {
	// contains filtered or unexported fields
}

GitManager manages the Git setup for the entire test suite. For each scenario, it provides a standardized, empty GitEnvironment consisting of a local and remote Git repository.

Setting up a GitEnvironment is an expensive operation and has to be done for every scenario. As a performance optimization, GitManager creates a fully set up GitEnvironment (including the main branch and configuration) (the "memoized" environment) at the beginning of the test suite and makes copies of it for each scenario. Making copies of a fully set up Git repo is much faster than creating it from scratch. End-to-end tests run multi-threaded, all threads share a global GitManager instance.

func NewGitManager

func NewGitManager(dir string) (GitManager, error)

NewGitManager provides a new GitManager instance operating in the given directory.

func (*GitManager) CreateScenarioEnvironment

func (manager *GitManager) CreateScenarioEnvironment(scenarioName string) (GitEnvironment, error)

CreateScenarioEnvironment provides a new GitEnvironment for the scenario with the given name.

type MockingRunner

type MockingRunner struct {

	// whether to log the output of subshell commands
	Debug bool `exhaustruct:"optional"`
	// contains filtered or unexported fields
}

MockingRunner runs shell commands using a customizable environment. This is useful in tests. Possible customizations:

  • overide environment variables
  • Temporarily override certain shell commands with mock implementations. Temporary mocks are only valid for the next command being run.

func (*MockingRunner) MockBrokenCommand

func (r *MockingRunner) MockBrokenCommand(name string) error

MockBrokenCommand adds a mock for the given command that returns an error.

func (*MockingRunner) MockCommand

func (r *MockingRunner) MockCommand(name string) error

MockCommand adds a mock for the command with the given name.

func (*MockingRunner) MockCommitMessage

func (r *MockingRunner) MockCommitMessage(message string) error

MockCommitMessage sets up this runner with an editor that enters the given commit message.

func (*MockingRunner) MockGit

func (r *MockingRunner) MockGit(version string) error

MockGit pretends that this repo has Git in the given version installed.

func (*MockingRunner) MockNoCommandsInstalled

func (r *MockingRunner) MockNoCommandsInstalled() error

MockNoCommandsInstalled pretends that no commands are installed.

func (*MockingRunner) Run

func (r *MockingRunner) Run(name string, arguments ...string) (string, error)

Run runs the given command with the given arguments. Overrides will be used and removed when done.

func (*MockingRunner) RunMany

func (r *MockingRunner) RunMany(commands [][]string) error

RunMany runs all given commands. Commands are provided as a list of argv-style strings. Overrides apply for the first command only. Failed commands abort immediately with the encountered error.

func (*MockingRunner) RunString

func (r *MockingRunner) RunString(fullCmd string) (string, error)

RunString runs the given command (including possible arguments). Overrides will be used and removed when done.

func (*MockingRunner) RunStringWith

func (r *MockingRunner) RunStringWith(fullCmd string, opts *Options) (string, error)

RunStringWith runs the given command (including possible arguments) using the given options. opts.Dir is a relative path inside the working directory of this ShellRunner. Overrides will be used and removed when done.

func (*MockingRunner) RunWith

func (r *MockingRunner) RunWith(opts *Options, cmd string, args ...string) (string, error)

RunWith runs the given command with the given options in this ShellRunner's directory.

func (*MockingRunner) SetTestOrigin

func (r *MockingRunner) SetTestOrigin(content string)

SetTestOrigin adds the given environment variable to subsequent runs of commands.

func (*MockingRunner) WorkingDir

func (r *MockingRunner) WorkingDir() string

WorkingDir provides the directory this MockingRunner operates in.

type Options

type Options struct {
	// Dir contains the directory in which to execute the command.
	// If empty, runs in the current directory.
	Dir string

	// Env allows to override the environment variables to use in the subshell, in the format provided by os.Environ()
	// If empty, uses the environment variables of this process.
	Env []string

	// Input contains the user input to enter into the running command.
	// It is written to the subprocess one element at a time, with a delay defined by command.InputDelay in between.
	Input []string // input into the subprocess
}

Options defines optional arguments for ShellRunner.RunWith().

type Runner

type Runner struct {
	Backend git.BackendCommands
	// contains filtered or unexported fields
}

Runner provides Git functionality for test code (unit and end-to-end tests).

func CreateRunner

func CreateRunner(t *testing.T) Runner

CreateRunner creates test.Runner instances.

func CreateTestGitTownRunner

func CreateTestGitTownRunner(t *testing.T) Runner

CreateTestGitTownRunner creates a test.Runner for use in tests, with a main branch and initial git town configuration.

func (*Runner) AddRemote

func (r *Runner) AddRemote(name, url string) error

AddRemote adds a Git remote with the given name and URL to this repository.

func (*Runner) AddSubmodule

func (r *Runner) AddSubmodule(url string) error

AddSubmodule adds a Git submodule with the given URL to this repository.

func (*Runner) BranchHierarchyTable

func (r *Runner) BranchHierarchyTable() DataTable

BranchHierarchyTable provides the currently configured branch hierarchy information as a DataTable.

func (*Runner) CheckoutBranch

func (r *Runner) CheckoutBranch(name string) error

CheckoutBranch checks out the Git branch with the given name in this repo.

func (*Runner) Clone

func (r *Runner) Clone(targetDir string) (Runner, error)

Clone creates a clone of the repository managed by this test.Runner into the given directory. The cloned repo uses the same homeDir and binDir as its origin.

func (*Runner) CommitStagedChanges

func (r *Runner) CommitStagedChanges(message string) error

CommitStagedChanges commits the currently staged changes.

func (*Runner) Commits

func (r *Runner) Commits(fields []string, mainBranch string) ([]git.Commit, error)

Commits provides a list of the commits in this Git repository with the given fields.

func (*Runner) CommitsInBranch

func (r *Runner) CommitsInBranch(branch string, fields []string) ([]git.Commit, error)

CommitsInBranch provides all commits in the given Git branch.

func (*Runner) ConnectTrackingBranch

func (r *Runner) ConnectTrackingBranch(name string) error

ConnectTrackingBranch connects the branch with the given name to its counterpart at origin. The branch must exist.

func (*Runner) CreateBranch

func (r *Runner) CreateBranch(name, parent string) error

CreateBranch creates a new branch with the given name. The created branch is a normal branch. To create feature branches, use CreateFeatureBranch.

func (*Runner) CreateChildFeatureBranch

func (r *Runner) CreateChildFeatureBranch(name string, parent string) error

CreateChildFeatureBranch creates a branch with the given name and parent in this repository. The parent branch must already exist.

func (*Runner) CreateCommit

func (r *Runner) CreateCommit(commit git.Commit) error

CreateCommit creates a commit with the given properties in this Git repo.

func (*Runner) CreateFile

func (r *Runner) CreateFile(name, content string) error

CreateFile creates a file with the given name and content in this repository.

func (*Runner) CreatePerennialBranches

func (r *Runner) CreatePerennialBranches(names ...string) error

CreatePerennialBranches creates perennial branches with the given names in this repository.

func (*Runner) CreateStandaloneTag

func (r *Runner) CreateStandaloneTag(name string) error

CreateStandaloneTag creates a tag not on a branch.

func (*Runner) CreateTag

func (r *Runner) CreateTag(name string) error

CreateTag creates a tag with the given name.

func (*Runner) DeleteMainBranchConfiguration

func (r *Runner) DeleteMainBranchConfiguration() error

DeleteMainBranchConfiguration removes the configuration for which branch is the main branch.

func (*Runner) Fetch

func (r *Runner) Fetch() error

Fetch retrieves the updates from the origin repo.

func (*Runner) FileContent

func (r *Runner) FileContent(filename string) (string, error)

FileContent provides the current content of a file.

func (*Runner) FileContentInCommit

func (r *Runner) FileContentInCommit(sha string, filename string) (string, error)

FileContentInCommit provides the content of the file with the given name in the commit with the given SHA.

func (*Runner) FilesInBranch

func (r *Runner) FilesInBranch(branch string) ([]string, error)

FilesInBranch provides the list of the files present in the given branch.

func (*Runner) FilesInBranches

func (r *Runner) FilesInBranches(mainBranch string) (DataTable, error)

FilesInBranches provides a data table of files and their content in all branches.

func (*Runner) FilesInCommit

func (r *Runner) FilesInCommit(sha string) ([]string, error)

FilesInCommit provides the names of the files that the commit with the given SHA changes.

func (*Runner) HasBranchesOutOfSync

func (r *Runner) HasBranchesOutOfSync() (bool, error)

HasBranchesOutOfSync indicates whether one or more local branches are out of sync with their tracking branch.

func (*Runner) HasFile

func (r *Runner) HasFile(name, content string) (bool, error)

HasFile indicates whether this repository contains a file with the given name and content.

func (*Runner) HasGitTownConfigNow

func (r *Runner) HasGitTownConfigNow() bool

HasGitTownConfigNow indicates whether this repository contain Git Town specific configuration.

func (*Runner) PushBranch

func (r *Runner) PushBranch() error

func (*Runner) PushBranchToRemote

func (r *Runner) PushBranchToRemote(branch, remote string) error

func (*Runner) RemoveBranch

func (r *Runner) RemoveBranch(name string) error

RemoveBranch deletes the branch with the given name from this repo.

func (*Runner) RemoveRemote

func (r *Runner) RemoveRemote(name string) error

RemoveRemote deletes the Git remote with the given name.

func (*Runner) RemoveUnnecessaryFiles

func (r *Runner) RemoveUnnecessaryFiles() error

RemoveUnnecessaryFiles trims all files that aren't necessary in this repo.

func (*Runner) ShaForCommit

func (r *Runner) ShaForCommit(name string) (string, error)

ShaForCommit provides the SHA for the commit with the given name.

func (*Runner) StageFiles

func (r *Runner) StageFiles(names ...string) error

StageFiles adds the file with the given name to the Git index.

func (*Runner) StashSize

func (r *Runner) StashSize() (int, error)

StashSize provides the number of stashes in this repository.

func (*Runner) Tags

func (r *Runner) Tags() ([]string, error)

Tags provides a list of the tags in this repository.

func (*Runner) UncommittedFiles

func (r *Runner) UncommittedFiles() ([]string, error)

UncommittedFiles provides the names of the files not committed into Git.

type ScenarioState

type ScenarioState struct {
	// contains filtered or unexported fields
}

ScenarioState constains the state that is shared by all steps within a scenario.

func (*ScenarioState) InitialBranches

func (state *ScenarioState) InitialBranches() DataTable

InitialBranches provides the branches in this Scenario before the WHEN steps ran.

func (*ScenarioState) Reset

func (state *ScenarioState) Reset(gitEnv GitEnvironment)

Reset restores the null value of this ScenarioState.

type TagTableBuilder

type TagTableBuilder struct {
	// contains filtered or unexported fields
}

TagTableBuilder collects data about tags in Git repositories in the same way that our Gherkin tables describing tags in repos are organized.

func NewTagTableBuilder

func NewTagTableBuilder() TagTableBuilder

NewTagTableBuilder provides a fully initialized instance of TagTableBuilder.

func (*TagTableBuilder) Add

func (builder *TagTableBuilder) Add(tag, location string)

Add registers the given tag from the given location into this table.

func (*TagTableBuilder) AddMany

func (builder *TagTableBuilder) AddMany(tags []string, location string)

AddMany registers the given tags from the given location into this table.

func (*TagTableBuilder) Table

func (builder *TagTableBuilder) Table() DataTable

Table provides the data accumulated by this TagTableBuilder as a DataTable.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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