linter

package
v0.0.0-...-a10075e Latest Latest
Warning

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

Go to latest
Published: May 23, 2021 License: GPL-3.0 Imports: 7 Imported by: 0

Documentation

Overview

Example (Linter_ExecuteLinter)
runLinter(New(
	"no files",
	config.LinterSettings{Command: []string{"echo"}},
))

runLinter(New(
	"files pattern",
	config.LinterSettings{
		Command: []string{"echo", "one"},
		Filters: config.Filters{Files: []string{"two", "three"}},
	},
))

runLinter(New(
	"folder pattern",
	config.LinterSettings{
		Command: []string{"echo", "one"},
		Filters: config.Filters{
			Folders: []string{"two", "three"},
		},
	},
))

runLinter(New(
	"git pattern",
	config.LinterSettings{
		Command: []string{"echo", "foo"},
		Filters: config.Filters{
			GitPattern: []string{"**"},
		},
	},
))

runLinter(New(
	"find pattern",
	config.LinterSettings{
		Command: []string{"echo", "foo"},
		Filters: config.Filters{
			FindPattern: []string{"*"},
		},
	},
))

runBadLinter(
	New(
		"git error",
		config.LinterSettings{
			Command: []string{"echo", "foo"},
			Filters: config.Filters{
				GitPattern: []string{""},
			},
		},
	),
	"cannot prepare linter 'git error' execution: exit status 128",
	-1,
)

runBadLinter(
	New(
		"bad linter",
		config.LinterSettings{
			Command: []string{"/nonexistant"},
			Filters: config.Filters{
				Folders: []string{"."},
			},
		},
	),
	"cannot execute linter 'bad linter': fork/exec /nonexistant: no such file or directory",
	-1,
)

{
	returnCode, err := New(
		"failed linter",
		config.LinterSettings{
			Command: []string{"sh"},
			Filters: config.Filters{
				Folders: []string{"-c", "exit 42"},
			},
		},
	).ExecuteLinter()
	if err != nil {
		panic(err)
	}
	if returnCode != 42 {
		panic(fmt.Sprint("bad return code: ", returnCode))
	}
}
Output:

================================================================================
===== Execute no files =========================================================
================================================================================
No file to lint. Just skip linter: no files
================================================================================
===== Execute files pattern ====================================================
================================================================================
one three two
================================================================================
===== Execute folder pattern ===================================================
================================================================================
one three two
================================================================================
===== Execute git pattern ======================================================
================================================================================
foo linter.go linter_test.go
================================================================================
===== Execute find pattern =====================================================
================================================================================
foo ./linter.go ./linter_test.go
================================================================================
===== Execute git error ========================================================
================================================================================
================================================================================
===== Execute bad linter =======================================================
================================================================================
================================================================================
===== Execute failed linter ====================================================
================================================================================
Example (Linter_PrintVersion)
l := New("no version", config.LinterSettings{})
if err := l.PrintVersion(); err != nil {
	panic(err)
}

l = New("foo bar", config.LinterSettings{Version: []string{"echo", "foo", "bar"}})
if err := l.PrintVersion(); err != nil {
	panic(err)
}

l = New("exit 1", config.LinterSettings{Version: []string{"sh", "-c", "echo 'The exit 1 version'; exit 1"}})
if err := l.PrintVersion(); err == nil || err.Error() != "cannot print 'exit 1' version: exit status 1" {
	panic(fmt.Sprint("not expected error: ", err))
}
Output:

================================================================================
===== Version of no version ====================================================
================================================================================
================================================================================
===== Version of foo bar =======================================================
================================================================================
foo bar
================================================================================
===== Version of exit 1 ========================================================
================================================================================
The exit 1 version

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Linter

type Linter interface {
	GetName() string
	PrintVersion() error
	ExecuteLinter() (returnCode int, err error)
}

Linter represent an executable linter.

func New

func New(name string, cfg config.LinterSettings) Linter

New instantiate an linter with the given configuration.

Jump to

Keyboard shortcuts

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