Улучшены модульные тесты.
Some checks failed
build / build_windows (push) Successful in 28s
build / build (push) Failing after 59s

This commit is contained in:
Алексей Бадяев 2024-11-21 10:26:33 +07:00
parent 74053d6dae
commit 36c7e7470d
Signed by: alexey
GPG Key ID: 686FBC1363E4AFAE
6 changed files with 97 additions and 147 deletions

8
cspell.config.yaml Normal file
View File

@ -0,0 +1,8 @@
version: "0.2"
ignorePaths:
- makefile
dictionaryDefinitions: []
dictionaries: []
words: []
ignoreWords: []
import: []

View File

@ -23,6 +23,8 @@ GO_OPT_V := -X "main.version=$(VERSION)"
GO_OPT_APP := -X "main.appname=$(PROJECT_NAME)"
GO_OPT_BASE := -mod vendor -ldflags '$(GO_OPT_V) $(GO_OPT_APP) $(GO_LDFLAGS)'
GO_OPT ?=
TEST_OPT ?= -race -v -shuffle=on -parallel 6
EXPORT_RESULT ?= false # for CI please set EXPORT_RESULT to true
COVERAGE_FORMAT ?= html
@ -30,19 +32,20 @@ GOCMD := go
GOTEST := $(GOCMD) test
GOVET := $(GOCMD) vet
ECHO_CMD := echo -e
TIMECMD ?= /usr/bin/time -f "Time elapsed:\t%E sec"
ifeq ($(OS),Windows_NT)
DIST_SUFFIX := windows-$(GOARCH)
MSI_FILE ?= $(PROJECT_ID)_$(VERSION)_$(GOARCH).msi
DIST_EXT := .zip
DIST_OPTS := -a -cf
MSI_VERSION := $(shell echo $(VERSION_NUMBER) | sed -e 's/-.*//')
BIN_SUFFIX := .exe
DIST_SUFFIX := windows-$(GOARCH)
MSI_FILE ?= $(PROJECT_ID)_$(VERSION)_$(GOARCH).msi
DIST_EXT := .zip
DIST_OPTS := -a -cf
MSI_VERSION := $(shell echo $(VERSION_NUMBER) | sed -e 's/-.*//')
BIN_SUFFIX := .exe
else
PLATFORM := $(shell uname -s | tr '[:upper:]' '[:lower:]')
DIST_SUFFIX := $(GOOS)-$(GOARCH)
DIST_EXT := .tar.gz
DIST_OPTS := -czf
PLATFORM := $(shell uname -s | tr '[:upper:]' '[:lower:]')
DIST_SUFFIX := $(GOOS)-$(GOARCH)
DIST_EXT := .tar.gz
DIST_OPTS := -czf
endif
PKG_NAME := $(PROJECT_ID)_$(VERSION)_$(DIST_SUFFIX)
@ -78,7 +81,7 @@ build: vendor $(addprefix $(BINDIR)/, $(APPS)) ## Build your project and put the
$(BINDIR)/%: cmd/%/main.go $(patsubst cmd/%/main.go,cmd/%/*.go,$<)
@rm -f "$(BINDIR)/$(BIN_PREFIX)$(patsubst cmd/%/main.go,%,$<)$(BIN_SUFFIX)"
GO111MODULE=on $(GOCMD) build $(GO_OPT_BASE) $(GO_OPT) \
GO111MODULE=on $(TIMECMD) $(GOCMD) build $(GO_OPT_BASE) $(GO_OPT) \
-o "$(BINDIR)/$(BIN_PREFIX)$(patsubst cmd/%/main.go,%,$<)$(BIN_SUFFIX)" \
$(patsubst %/main.go,./%,$<)
@ -103,9 +106,9 @@ clean: ## Remove build related files
## Package
ifeq ($(OS),Windows_NT)
PACKAGE_TARGETS := pkg-msi
PACKAGE_TARGETS := pkg-msi
else
PACKAGE_TARGETS := pkg-deb
PACKAGE_TARGETS := pkg-deb
endif
package: $(PACKAGE_TARGETS) ## Build all available packages
@ -175,7 +178,7 @@ ifeq ($(EXPORT_RESULT), true)
@mkdir -p $(OUTDIR)
$(eval OUTPUT_OPTIONS = | go-junit-report -set-exit-code > $(OUTDIR)/junit-report.xml)
endif
$(GOTEST) -v $(GO_OPT) ./... $(OUTPUT_OPTIONS)
$(TIMECMD) $(GOTEST) $(TEST_OPT) ./... $(OUTPUT_OPTIONS)
@$(ECHO_CMD) "Test\t\t${GREEN}[OK]${RESET}"
.PHONY:test

View File

@ -327,13 +327,19 @@ func init() {
}
func TestDiagrams(t *testing.T) {
assert := assert.New(t)
for _, test := range testData {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
for filePath, source := range test.files {
t.Run(filePath, func(t *testing.T) {
t.Parallel()
diagrams, err := drawio.Diagrams(strings.NewReader(source.data))
assert.NoError(t, err)
assert.ElementsMatch(t, source.diagrams, diagrams)
assert.NoError(err)
assert.ElementsMatch(source.diagrams, diagrams)
})
}
})
@ -341,14 +347,22 @@ func TestDiagrams(t *testing.T) {
}
func TestExportFile(t *testing.T) {
assert := assert.New(t)
for _, test := range testData {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
for filePath, source := range test.files {
var openOpt drawio.Option = drawio.WithNop()
if len(source.output) > 0 {
openOpt = drawio.WithOutput(source.output)
}
t.Run(filePath, func(t *testing.T) {
t.Parallel()
var (
openFileCalls = []string{}
exp = drawio.New(
@ -359,12 +373,15 @@ func TestExportFile(t *testing.T) {
openOpt,
)
)
commands, err := exp.ExportFile(filePath, []string{})
assert.NoError(t, err)
if assert.Equal(t, 1, len(openFileCalls)) {
assert.Equal(t, filePath, openFileCalls[0])
assert.NoError(err)
if assert.Equal(1, len(openFileCalls)) {
assert.Equal(filePath, openFileCalls[0])
}
assert.ElementsMatch(t, source.commands, cmd2info(commands))
assert.ElementsMatch(source.commands, cmd2info(commands))
})
}
})
@ -372,11 +389,14 @@ func TestExportFile(t *testing.T) {
}
func TestExportDir(t *testing.T) {
assert := assert.New(t)
for _, recursive := range []bool{false, true} {
var (
name string
recursiveOption drawio.Option
)
if recursive {
name = "recursive"
recursiveOption = drawio.WithRecursive()
@ -384,9 +404,14 @@ func TestExportDir(t *testing.T) {
name = "non-recursive"
recursiveOption = drawio.WithNop()
}
t.Run(name, func(t *testing.T) {
t.Parallel()
for _, test := range testData {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
var (
openFileCalls = []string{}
exp = drawio.New(
@ -402,17 +427,20 @@ func TestExportDir(t *testing.T) {
recursiveOption,
)
)
for dir := range test.dirs {
pathDir, _ := path.Split(dir)
if len(pathDir) > 0 {
if pathDir != "" {
continue
}
t.Run(dir, func(t *testing.T) {
t.Parallel()
openFileCalls = openFileCalls[:0]
commands, err := exp.ExportDir(dir, []string{})
assert.NoError(t, err)
assert.ElementsMatch(t,
test.dirCommands(dir, recursive), cmd2info(commands))
assert.NoError(err)
assert.ElementsMatch(test.dirCommands(dir, recursive), cmd2info(commands))
})
}
})

View File

@ -10,6 +10,8 @@ import (
)
func TestOptions(t *testing.T) {
assert := assert.New(t)
testData := []struct {
name string // Наименование теста
opts []Option // Параметры
@ -105,16 +107,21 @@ func TestOptions(t *testing.T) {
},
},
}
for _, test := range testData {
t.Run(test.name, func(t *testing.T) {
t.Parallel()
options := Options{}
for _, opt := range test.opts {
opt.apply(&options)
}
assert.Equal(t, test.app, options.App())
assert.Equal(t, test.outdir, options.OutDir())
assert.Equal(t, test.outext, options.OutExt())
assert.ElementsMatch(t, test.args, options.Args())
assert.Equal(test.app, options.App())
assert.Equal(test.outdir, options.OutDir())
assert.Equal(test.outext, options.OutExt())
assert.ElementsMatch(test.args, options.Args())
})
}
}
@ -125,8 +132,12 @@ func openFile(name string) (io.ReadCloser, error) {
}
func TestOptionsOpenFileFunc(t *testing.T) {
t.Parallel()
assert := assert.New(t)
opts := New(WithOpenFile(openFile))
assert.Equal(t,
assert.Equal(
reflect.ValueOf(openFile).Pointer(),
reflect.ValueOf(opts.openFile).Pointer(),
)
@ -137,8 +148,12 @@ func readDir(name string) ([]os.DirEntry, error) {
}
func TestOptionsReadDirFunc(t *testing.T) {
t.Parallel()
assert := assert.New(t)
opts := New(WithReadDir(readDir))
assert.Equal(t,
assert.Equal(
reflect.ValueOf(readDir).Pointer(),
reflect.ValueOf(opts.readDir).Pointer(),
)

View File

@ -8,6 +8,8 @@ import (
)
func TestFormat(t *testing.T) {
assert := assert.New(t)
testData := []struct {
name string
err error
@ -57,19 +59,21 @@ func TestFormat(t *testing.T) {
ext: "",
},
}
for _, test := range testData {
t.Run(test.name, func(t *testing.T) {
var (
v drawio.Format
err error
)
err = (&v).Set(test.name)
t.Parallel()
var v drawio.Format
err := (&v).Set(test.name)
if test.err == nil {
assert.Equal(t, test.name, test.format.String())
assert.NoError(t, err)
assert.Equal(test.name, test.format.String())
assert.NoError(err)
} else {
assert.ErrorIs(t, err, test.err)
assert.ErrorContains(t, err, test.err.Error())
assert.ErrorIs(err, test.err)
assert.ErrorContains(err, test.err.Error())
}
})
}

View File

@ -1,108 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="5.0">
<file name="cmd/drawio-export/main.go">
<error column="2" line="8" message="import &#39;git.mousesoft.ru/ms/drawio-exporter/pkg/drawio&#39; is not allowed from list &#39;Main&#39;" severity="error" source="depguard"></error>
<error column="3" line="23" message="use of `fmt.Println` forbidden by pattern `^(fmt\.Print(|f|ln)|print|println)$`" severity="error" source="forbidigo"></error>
<error column="2" line="12" message="appname is a global variable" severity="error" source="gochecknoglobals"></error>
</file>
<file name="cmd/drawio-export/options.go">
<error column="2" line="6" message="import &#39;git.mousesoft.ru/ms/drawio-exporter/pkg/drawio&#39; is not allowed from list &#39;Main&#39;" severity="error" source="depguard"></error>
<error column="21" line="13" message="drawio.Options is missing fields Application, EnableXvfb, Output, Format, Recursive, RemovePageSuffix, Quality, Transparent, EmbedDiagram, EmbedSvgImages, Border, Scale, Width, Height, Crop, Uncompressed, EnablePlugins" severity="error" source="exhaustruct"></error>
<error column="0" line="16" message="Function &#39;init&#39; is too long (65 &gt; 60)" severity="error" source="funlen"></error>
<error column="2" line="10" message="flagHelp is a global variable" severity="error" source="gochecknoglobals"></error>
<error column="2" line="11" message="flagVersion is a global variable" severity="error" source="gochecknoglobals"></error>
<error column="2" line="12" message="flagIgnoreErrors is a global variable" severity="error" source="gochecknoglobals"></error>
<error column="2" line="13" message="opts is a global variable" severity="error" source="gochecknoglobals"></error>
<error column="1" line="16" message="don&#39;t use `init` function" severity="error" source="gochecknoinits"></error>
<error column="41" line="18" message="string `Prints version information` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="46" line="21" message="string `Ignore Draw.IO errors` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="45" line="24" message="string `Draw.io Desktop Application` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="45" line="27" message="string `Run drawio inside xvfb-run` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="40" line="30" message="string `Exported folder name [default: export]` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="3" line="34" message="string `Exported format [default: pdf] [possible values: pdf, png, jpg, svg, vsdx, xml]` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="3" line="39" message="string `For a folder input, recursively convert all files in sub-folders also` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="38" line="46" message="string `Output image quality for JPEG [default: 90]` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="46" line="49" message="string `Set transparent background for PNG` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="3" line="53" message="string `Includes a copy of the diagram for PDF, PNG, or SVG` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="3" line="60" message="string `Sets the border width around the diagram [default: 0]` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="36" line="64" message="string `Scales the diagram size` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="47" line="75" message="string `Uncompressed XML output` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="38" line="80" message="string `Prints help information` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
</file>
<file name="pkg/drawio/execution.go">
<error column="0" line="1" message="Missed header for check" severity="error" source="goheader"></error>
<error column="2" line="19" message="return with no blank line before" severity="error" source="nlreturn"></error>
<error column="2" line="14" message="ranges should only be cuddled with assignments used in the iteration" severity="error" source="wsl"></error>
<error column="2" line="19" message="return statements should not be cuddled if block has more than two lines" severity="error" source="wsl"></error>
</file>
<file name="pkg/drawio/export.go">
<error column="2" line="14" message="import &#39;github.com/tamerh/xml-stream-parser&#39; is not allowed from list &#39;Main&#39;" severity="error" source="depguard"></error>
<error column="14" line="19" message="drawio.Options is missing fields EnableXvfb, Recursive, RemovePageSuffix, Quality, Transparent, EmbedDiagram, EmbedSvgImages, Border, Scale, Width, Height, Crop, Uncompressed, EnablePlugins, openFile, readDir" severity="error" source="exhaustruct"></error>
<error column="9" line="32" message="drawio.Exporter is missing fields openFile, readDir" severity="error" source="exhaustruct"></error>
<error column="23" line="22" message="string `pdf` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="25" line="176" message="string `diagram` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="17" line="41" message="unlambda: replace `func(name string) ([]os.DirEntry, error) {&#xA;&#x9;return os.ReadDir(name)&#xA;}` with `os.ReadDir`" severity="error" source="gocritic"></error>
<error column="0" line="1" message="Missed header for check" severity="error" source="goheader"></error>
<error column="53" line="152" message="Magic number: 10, in &lt;argument&gt; detected" severity="error" source="mnd"></error>
<error column="2" line="27" message="return with no blank line before" severity="error" source="nlreturn"></error>
<error column="2" line="47" message="return with no blank line before" severity="error" source="nlreturn"></error>
<error column="4" line="69" message="continue with no blank line before" severity="error" source="nlreturn"></error>
<error column="6" line="146" message="variable name &#39;i&#39; is too short for the scope of its usage" severity="error" source="varnamelen"></error>
<error column="2" line="66" message="ranges should only be cuddled with assignments used in the iteration" severity="error" source="wsl"></error>
<error column="3" line="71" message="declarations should never be cuddled" severity="error" source="wsl"></error>
<error column="3" line="72" message="only one cuddle assignment allowed before if statement" severity="error" source="wsl"></error>
<error column="3" line="77" message="append only allowed to cuddle with appended value" severity="error" source="wsl"></error>
<error column="3" line="78" message="only one cuddle assignment allowed before if statement" severity="error" source="wsl"></error>
<error column="2" line="82" message="if statements should only be cuddled with assignments" severity="error" source="wsl"></error>
<error column="2" line="85" message="return statements should not be cuddled if block has more than two lines" severity="error" source="wsl"></error>
<error column="2" line="101" message="only one cuddle assignment allowed before range statement" severity="error" source="wsl"></error>
<error column="3" line="103" message="declarations should never be cuddled" severity="error" source="wsl"></error>
<error column="3" line="104" message="only one cuddle assignment allowed before if statement" severity="error" source="wsl"></error>
<error column="3" line="117" message="append only allowed to cuddle with appended value" severity="error" source="wsl"></error>
<error column="2" line="138" message="only one cuddle assignment allowed before defer statement" severity="error" source="wsl"></error>
<error column="2" line="139" message="if statements should only be cuddled with assignments" severity="error" source="wsl"></error>
<error column="2" line="142" message="assignments should only be cuddled with other assignments" severity="error" source="wsl"></error>
<error column="2" line="146" message="only one cuddle assignment allowed before range statement" severity="error" source="wsl"></error>
<error column="3" line="151" message="assignments should only be cuddled with other assignments" severity="error" source="wsl"></error>
<error column="3" line="163" message="assignments should only be cuddled with other assignments" severity="error" source="wsl"></error>
<error column="2" line="166" message="return statements should not be cuddled if block has more than two lines" severity="error" source="wsl"></error>
<error column="2" line="178" message="ranges should only be cuddled with assignments used in the iteration" severity="error" source="wsl"></error>
<error column="3" line="182" message="if statements should only be cuddled with assignments" severity="error" source="wsl"></error>
</file>
<file name="pkg/drawio/options.go">
<error column="1" line="136" message="calculated cyclomatic complexity for function Args is 15, max is 10" severity="error" source="cyclop"></error>
<error column="6" line="17" message="the type name `ErrUnsupportedFormat` should conform to the `XxxError` format" severity="error" source="errname"></error>
<error column="16" line="414" message="drawio.optionOpenFile is missing field openFile" severity="error" source="exhaustruct"></error>
<error column="16" line="429" message="drawio.optionReadDir is missing field readDir" severity="error" source="exhaustruct"></error>
<error column="10" line="112" message="string `drawio` has 2 occurrences, make it a constant" severity="error" source="goconst"></error>
<error column="2" line="28" message="singleCaseSwitch: should rewrite switch statement to if statement" severity="error" source="gocritic"></error>
<error column="0" line="1" message="Missed header for check" severity="error" source="goheader"></error>
<error column="14" line="77" message="fieldalignment: struct of size 136 could be 120" severity="error" source="govet"></error>
<error column="1" line="187" message="WithAppPath returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="200" message="WithXvfb returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="213" message="WithOutput returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="226" message="WithFormat returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="237" message="WithRecursive returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="250" message="WithRemovePageSuffix returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="263" message="WithQuality returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="276" message="WithTransparent returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="289" message="WithEmbedDiagram returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="302" message="WithEmbedSvgImages returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="315" message="WithBorder returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="328" message="WithScale returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="341" message="WithWidth returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="354" message="WithHeight returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="367" message="WithCrop returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="380" message="WithUncompressed returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="393" message="WithEnablePlugins returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="406" message="WithOpenFile returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="421" message="WithReadDir returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="436" message="WithNop returns interface (git.mousesoft.ru/ms/drawio-exporter/pkg/drawio.Option)" severity="error" source="ireturn"></error>
<error column="1" line="232" message="receiver-naming: receiver name opt should be consistent with previous receiver name f for Format" severity="warning" source="revive"></error>
<error column="28" line="444" message="unused-parameter: parameter &#39;opts&#39; seems to be unused, consider removing or renaming it as _" severity="warning" source="revive"></error>
<error column="18" line="54" message="ST1016: methods on the same type should have the same receiver name (seen 1x &#34;opt&#34;, 3x &#34;f&#34;)" severity="error" source="stylecheck"></error>
<error column="2" line="138" message="if statements should only be cuddled with assignments used in the if statement itself" severity="error" source="wsl"></error>
<error column="3" line="145" message="append only allowed to cuddle with appended value" severity="error" source="wsl"></error>
</file>
</checkstyle>