qac

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2023 License: Apache-2.0 Imports: 16 Imported by: 0

README

qac

CI Linux CI Windows PkgGoDev Go Report Card

qac is a Go library to test end to end command line tools.

A test plan is written in YAML format.

preconditions:
  fs:
    - file: ../go.mod
specs:
  cat:
    command:
      working_dir: ../
      cli: cat go.mod
    expectations:
      status:
        equals_to: 0
      output:
        stdout:
          equals_to_file: ../go.mod

Usage in Go tests:

import (
  "testing"
  "github.com/enr/qac"
)
func TestExecution(t *testing.T) {
  launcher := qac.NewLauncher()
  report := launcher.ExecuteFile(`/path/to/qac.yaml`)
  // Not needed but useful to see what's happening
  reporter := qac.NewTestLogsReporter(t)
  reporter.Publish(report)
  // Fail test if any error is found
  for _, err := range report.AllErrors() {
    t.Errorf(`error %v`, err)
  }
}

Programmatic usage:

  // the commmand to test
  command := qac.Command{
    Exe: "echo",
    Args: []string{
      `foo`,
    },
  }

  // expectations about its result
  stdErrEmpty := true
  expectations := qac.Expectations{
    StatusAssertion: qac.StatusAssertion{
      EqualsTo: "0",
    },
    OutputAssertions: qac.OutputAssertions{
      Stdout: qac.OutputAssertion{
        EqualsTo: `foo`,
      },
      Stderr: qac.OutputAssertion{
        IsEmpty: &stdErrEmpty,
      },
    },
  }

  // build the full specs structure
  spec := qac.Spec{
    Command:      command,
    Expectations: expectations,
  }
  specs := make(map[string]qac.Spec)
  specs[`echo`] = spec

  // add specs to test plan
  plan := qac.TestPlan{
    Specs: specs,
  }

  // run the plan
  launcher := qac.NewLauncher()

  // see results
  report := launcher.Execute(plan)
  for _, block := range report.Blocks() {
    for _, entry := range block.Entries() {
      fmt.Printf(" - %s %s %v \n", entry.Kind().String(), entry.Description(), entry.Errors())
    }
  }

License

Apache 2.0 - see LICENSE file.

Copyright 2020-TODAY qac contributors

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AssertionResult

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

AssertionResult is the container of verification results.

func (*AssertionResult) Description

func (r *AssertionResult) Description() string

Description is the textual representation of the assertion.

func (*AssertionResult) Errors

func (r *AssertionResult) Errors() []error

Errors returns the errors list.

func (*AssertionResult) Success

func (r *AssertionResult) Success() bool

Success returns if an assertion completed with no error.

type Command

type Command struct {
	WorkingDir string `yaml:"working_dir"`
	Cli        string `yaml:"cli"`
	Exe        string `yaml:"exe"`
	Env        map[string]string
	// added to exe
	Extension FileExtension `yaml:"ext"`
	Args      []string      `yaml:"args"`
}

Command represents the command under test.

func (Command) String

func (c Command) String() string

type DirectoryAssertion

type DirectoryAssertion struct {
	Path            string   `yaml:"path"`
	Exists          bool     `yaml:"exists"`
	EqualsTo        string   `yaml:"equals_to"`
	ContainsAny     []string `yaml:"contains_any"`
	ContainsAll     []string `yaml:"contains_all"`
	ContainsExactly []string `yaml:"contains_exactly"`
}

DirectoryAssertion is an assertion on a given directory.

type Expectations

type Expectations struct {
	StatusAssertion      StatusAssertion       `yaml:"status"`
	OutputAssertions     OutputAssertions      `yaml:"output"`
	FileSystemAssertions []FileSystemAssertion `yaml:"fs"`
}

Expectations is the aggregate of the final assertions on the command executed.

type FileAssertion

type FileAssertion struct {
	Path string `yaml:"path"`
	// aggiunta a path
	Extension    FileExtension `yaml:"ext"`
	Exists       bool          `yaml:"exists"`
	EqualsTo     string        `yaml:"equals_to"`
	TextEqualsTo string        `yaml:"text_equals_to"`
	ContainsAny  []string      `yaml:"contains_any"`
	ContainsAll  []string      `yaml:"contains_all"`
}

FileAssertion is an assertion on a given file.

type FileExtension

type FileExtension struct {
	Windows string `yaml:"windows"`
	Unix    string `yaml:"unix"`
}

FileExtension is added as suffix to file assertions' path and command's exe values based on runtime.GOOS

type FileSystemAssertion

type FileSystemAssertion struct {
	File string `yaml:"file"`
	// aggiunta a file
	Extension FileExtension `yaml:"ext"`
	Directory string        `yaml:"directory"`
	Exists    *bool         `yaml:"exists"`
	EqualsTo  string        `yaml:"equals_to"`
	// Only for files
	TextEqualsTo    string   `yaml:"text_equals_to"`
	ContainsAny     []string `yaml:"contains_any"`
	ContainsAll     []string `yaml:"contains_all"`
	ContainsExactly []string `yaml:"contains_exactly"`
}

FileSystemAssertion is an assertion on files and directories.

type Launcher

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

Launcher checks the results respect expectations.

func NewLauncher

func NewLauncher() *Launcher

NewLauncher creates a default implementation for Launcher.

func (*Launcher) Execute

func (l *Launcher) Execute(plan TestPlan) *TestExecutionReport

Execute run tests loaded from a TestPlan.

func (*Launcher) ExecuteFile

func (l *Launcher) ExecuteFile(path string) *TestExecutionReport

ExecuteFile run tests loaded from a file.

type OutputAssertion

type OutputAssertion struct {
	EqualsTo     string `yaml:"equals_to"`
	EqualsToFile string `yaml:"equals_to_file"`
	// output is trimmed
	StartsWith string `yaml:"starts_with"`
	// output is trimmed
	EndsWith     string   `yaml:"ends_with"`
	IsEmpty      *bool    `yaml:"is_empty"`
	ContainsAny  []string `yaml:"contains_any"`
	ContainsAll  []string `yaml:"contains_all"`
	ContainsNone []string `yaml:"contains_none"`
	// contains filtered or unexported fields
}

OutputAssertion is an assertion on the output of a command: namely standard output and standard error.

type OutputAssertions

type OutputAssertions struct {
	Stdout OutputAssertion `yaml:"stdout"`
	Stderr OutputAssertion `yaml:"stderr"`
}

OutputAssertions is the aggregate of stdout and stderr assertions.

type Preconditions

type Preconditions struct {
	FileSystemAssertions []FileSystemAssertion `yaml:"fs"`
}

Preconditions represents the minimal requirements for a plan or a single spec to start.

type ReportBlock

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

ReportBlock is an aggregate of report entries classified on the phase.

func (*ReportBlock) Entries

func (r *ReportBlock) Entries() []ReportEntry

Entries returns the entries of a block.

func (*ReportBlock) Phase

func (r *ReportBlock) Phase() string

Phase returns the phase of a block.

type ReportEntry

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

ReportEntry is a single unit of information in a report.

func (*ReportEntry) Description

func (r *ReportEntry) Description() string

Description is the textual representation of a report entry.

func (*ReportEntry) Errors

func (r *ReportEntry) Errors() []error

Errors returns the errors list in a report entry.

func (*ReportEntry) Kind

func (r *ReportEntry) Kind() ReportEntryType

Kind returns the type of a report entry: error, info, success...

type ReportEntryType

type ReportEntryType int8

ReportEntryType represents the type of a report entry.

const (

	// ErrorType is report entry error
	ErrorType ReportEntryType
	// InfoType is report entry info
	InfoType
	// SuccessType is report entry success
	SuccessType
)

type Reporter

type Reporter interface {
	Publish(report *TestExecutionReport) error
}

Reporter is the interface for components publishing the report.

func NewConsoleReporter

func NewConsoleReporter() Reporter

NewConsoleReporter returns a Reporter implementation writing to the stdout.

func NewTestLogsReporter

func NewTestLogsReporter(t *testing.T) Reporter

NewTestLogsReporter returns a Reporter implementation using the testing log.

type Spec

type Spec struct {
	Description   string        `yaml:"description"`
	Preconditions Preconditions `yaml:"preconditions"`
	Command       Command       `yaml:"command"`
	Expectations  Expectations  `yaml:"expectations"`
	// contains filtered or unexported fields
}

Spec is the single test.

func (Spec) ID

func (s Spec) ID() string

ID returns the dinamically created identifier for a spec.

type StatusAssertion

type StatusAssertion struct {
	EqualsTo    string `yaml:"equals_to"`
	GreaterThan string `yaml:"greater_than"`
	LesserThan  string `yaml:"lesser_than"`
}

StatusAssertion represents an assertion on the status code returned from a command.

type TestExecutionReport

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

TestExecutionReport is the full report on a test execution

func (*TestExecutionReport) AllErrors

func (r *TestExecutionReport) AllErrors() []error

AllErrors returns all errors in a report, without considering blocks or phases.

func (*TestExecutionReport) Blocks

func (r *TestExecutionReport) Blocks() []*ReportBlock

Blocks returns the blocks list in a full report.

type TestPlan

type TestPlan struct {
	Preconditions Preconditions   `yaml:"preconditions"`
	Specs         map[string]Spec `yaml:"specs"`
}

TestPlan represents the full set of tests on a program.

Jump to

Keyboard shortcuts

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