Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Go #42

Merged
merged 4 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 6 additions & 44 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,7 @@ on:
pull_request:
jobs:
shellcheck:
name: shellcheck
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: shellcheck
uses: fkautz/shell-linter@v1.0.1
uses: networkservicemesh/.github/.github/workflows/shellcheck.yaml@main

build-and-test:
name: build-and-test
Expand All @@ -25,7 +20,7 @@ jobs:
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.16
go-version: 1.20.5
- name: Setup envs
run: |
echo GOPATH=$GITHUB_WORKSPACE >> $GITHUB_ENV
Expand All @@ -43,43 +38,10 @@ jobs:
working-directory: ${{ github.workspace }}/src/github.com/${{ github.repository }}

golangci-lint:
name: golangci-lint
runs-on: ubuntu-latest
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.16
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.35
uses: networkservicemesh/.github/.github/workflows/golangci-lint.yaml@main

excludeFmtErrorf:
name: exclude fmt.Errorf
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Exclude fmt.Errorf
run: |
if grep -r --include=*.go --exclude=*.pb.go fmt.Errorf . ; then
echo "Please use errors.Errorf (or errors.New or errors.Wrap or errors.Wrapf) as appropriate rather than fmt.Errorf"
exit 1
fi
uses: networkservicemesh/.github/.github/workflows/exclude-fmt-errorf.yaml@main

checkgomod:
name: check go.mod and go.sum
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-go@v1
with:
go-version: 1.16
- run: go mod tidy
- name: Check for changes in go.mod or go.sum
run: |
git diff --name-only --exit-code go.mod || ( echo "Run go tidy" && false )
git diff --name-only --exit-code go.sum || ( echo "Run go tidy" && false )
uses: networkservicemesh/.github/.github/workflows/checkgomod.yaml@main
17 changes: 6 additions & 11 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
run:
# concurrency: 6
go: "1.20"
timeout: 2m
issues-exit-code: 1
tests: true
Expand Down Expand Up @@ -41,13 +41,11 @@ linters-settings:
min-len: 2
min-occurrences: 2
depguard:
list-type: blacklist
include-go-root: false
packages:
- errors
packages-with-error-message:
# specify an error message to output when a blacklisted package is used
- errors: "Please use \"github.com/pkg/errors\" instead of \"errors\" in go imports"
rules:
main:
deny:
- pkg: errors
desc: Please use github.com/pkg/errors instead of errors in go imports
misspell:
locale: US
unparam:
Expand Down Expand Up @@ -128,7 +126,6 @@ linters-settings:
linters:
disable-all: true
enable:
# - rowserrcheck
- goheader
- bodyclose
- deadcode
Expand All @@ -149,7 +146,6 @@ linters:
- govet
- ineffassign
- interfacer
# - lll
- misspell
- nakedret
- scopelint
Expand All @@ -159,7 +155,6 @@ linters:
- typecheck
- unconvert
- unparam
# - unused
- varcheck
- whitespace
issues:
Expand Down
14 changes: 13 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/networkservicemesh/gotestmd

go 1.16
go 1.20

require (
github.com/pkg/errors v0.9.1
Expand All @@ -9,3 +9,15 @@ require (
github.com/stretchr/testify v1.6.1
go.uber.org/goleak v1.1.10
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/lint v0.0.0-20190930215403-16217165b5de // indirect
golang.org/x/sys v0.5.0 // indirect
golang.org/x/text v0.10.0
golang.org/x/tools v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ go.uber.org/goleak v1.1.10/go.mod h1:8a7PlsEVH3e/a/GLqe5IIrQx6GzcnRmZEufDUTk4A7A
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de h1:5hukYrvBGR8/eNkX5mdUezrA6JiaEZDtJb9Ei+1LlBs=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.8.0 h1:LUYupSeNrTNCGzR/hVBk2NHZO4hXcVaW1k4Qx7rjPx8=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.10.0 h1:UpjohKhiEgNc0CSauXmwYftY1+LlaC75SJwh0SgCX58=
golang.org/x/text v0.10.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11 h1:Yq9t9jnGoR+dBuitxdo9l6Q7xh/zOyNnYUtDKaQ3x0E=
golang.org/x/tools v0.0.0-20191108193012-7d206e10da11/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.6.0 h1:BOw41kyTf3PuCW1pVQf8+Cyg8pMlkYB1oo9iJ6D/lKM=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
Expand Down
Binary file added gotestmd
Binary file not shown.
7 changes: 6 additions & 1 deletion internal/generator/generator.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -21,6 +23,9 @@ import (
"path/filepath"
"strings"

"golang.org/x/text/cases"
"golang.org/x/text/language"

"github.com/networkservicemesh/gotestmd/internal/config"
"github.com/networkservicemesh/gotestmd/internal/linker"
)
Expand Down Expand Up @@ -50,7 +55,7 @@ func (g *Generator) Generate(examples ...*linker.LinkedExample) []*Suite {
for _, parent := range e.Parents {
tests[parent.Name] = append(tests[parent.Name], &Test{
Dir: e.Dir,
Name: strings.Title(nameRegex.ReplaceAllString(name, "_")),
Name: cases.Title(language.AmericanEnglish).String(nameRegex.ReplaceAllString(name, "_")),
Cleanup: e.Cleanup,
Run: e.Run,
})
Expand Down
7 changes: 5 additions & 2 deletions internal/generator/suite.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) 2020-2022 Doc.ai and/or its affiliates.
//
// Copyright (c) 2022 Cisco and/or its affiliates.
// Copyright (c) 2022-2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
Expand All @@ -23,6 +23,9 @@ import (
"path"
"strings"
"text/template"

"golang.org/x/text/cases"
"golang.org/x/text/language"
)

const suiteTemplate = `// Code generated by gotestmd DO NOT EDIT.
Expand Down Expand Up @@ -121,7 +124,7 @@ func (s *Suite) generateChildrenTesting() string {
var suites []*suiteData
for _, child := range s.Children {
_, title := path.Split(child.Dir)
title = strings.Title(nameRegex.ReplaceAllString(title, "_"))
title = cases.Title(language.AmericanEnglish).String(nameRegex.ReplaceAllString(title, "_"))
suite := &suiteData{
Title: title,
Name: child.Name(),
Expand Down
5 changes: 3 additions & 2 deletions internal/generator/utils.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -17,7 +19,6 @@
package generator

import (
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -55,7 +56,7 @@ func moduleName(start string) string {
for len(currDir) > 0 {
p := filepath.Clean(filepath.Join(currDir, "go.mod"))
if _, err = os.Open(p); err == nil {
source, err := ioutil.ReadFile(p)
source, err := os.ReadFile(p)
if err != nil {
logrus.Fatal(err.Error())
}
Expand Down
5 changes: 3 additions & 2 deletions internal/parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -19,7 +21,6 @@ package parser

import (
"io"
"io/ioutil"
"os"
"path/filepath"
"regexp"
Expand Down Expand Up @@ -57,7 +58,7 @@ func (p *Parser) ParseFile(filePath string) (*Example, error) {

// Parse reads io.Reader
func (p *Parser) Parse(r io.Reader) (*Example, error) {
bytes, err := ioutil.ReadAll(r)
bytes, err := io.ReadAll(r)
if err != nil {
return nil, err
}
Expand Down
17 changes: 8 additions & 9 deletions pkg/bash/bash.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2020-2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -83,7 +85,7 @@ func (b *Bash) Dir() string {

// Init initializes all resources for the bash runner.
//
// Must be called before any call to Run or Close
// # Must be called before any call to Run or Close
//
// You are advised to use bash.New instead, which calls this function automatically.
func (b *Bash) Init() error {
Expand Down Expand Up @@ -166,27 +168,24 @@ func (b *Bash) extractMessagesFromPipe(pipe io.Reader, ch chan string) {
// Run runs the command
func (b *Bash) Run(cmd string) (stdout, stderr string, exitCode int, err error) {
if b.ctx.Err() != nil {
err = b.ctx.Err()
return
return "", "", 0, b.ctx.Err()
}

_, err = b.stdin.Write([]byte(cmd + "\n" + cmdPrintStatusCode + "\n" + cmdPrintStdoutFinish + "\n" + cmdPrintStderrFinish + "\n"))
if err != nil {
return
return "", "", 0, err
}

select {
case stdout = <-b.stdoutCh:
case <-b.ctx.Done():
err = b.ctx.Err()
return
return "", "", 0, nil
}

select {
case stderr = <-b.stderrCh:
case <-b.ctx.Done():
err = b.ctx.Err()
return
return "", "", 0, nil
}

lastLineBreak := strings.LastIndex(stdout, "\n")
Expand All @@ -201,7 +200,7 @@ func (b *Bash) Run(cmd string) (stdout, stderr string, exitCode int, err error)
var exitCode64 int64
exitCode64, err = strconv.ParseInt(exitCodeString, 0, 9)
if err != nil {
return
return "", "", 0, err
}
exitCode = int(exitCode64)

Expand Down
3 changes: 1 addition & 2 deletions pkg/bash/bash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package bash_test

import (
"io/ioutil"
"math/rand"
"os"
"testing"
Expand Down Expand Up @@ -85,7 +84,7 @@ EOF`)
require.Empty(t, stdout)
require.Empty(t, stderr)

content, err := ioutil.ReadFile("test")
content, err := os.ReadFile("test")
require.NoError(t, err)
require.Equal(t, envValue+"\n", string(content))
_ = os.Remove("test")
Expand Down
6 changes: 4 additions & 2 deletions pkg/suites/shell/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright (c) 2021 Doc.ai and/or its affiliates.
//
// Copyright (c) 2023 Cisco and/or its affiliates.
//
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -40,7 +42,7 @@ func TestShellFirstTry(t *testing.T) {
fileName := "TestShellFirstTry.file"

r.Run("echo " + fileContent + " >" + fileName)
bytes, err := os.ReadFile(filepath.Join(tempDir, fileName))
bytes, err := os.ReadFile(filepath.Clean(filepath.Join(tempDir, fileName)))
require.NoError(t, err)
require.Equal(t, fileContent+"\n", string(bytes))
}
Expand All @@ -63,7 +65,7 @@ func TestShellEventually(t *testing.T) {
echo >&2 not enough ones
false
fi`)
bytes, err := os.ReadFile(filepath.Join(tempDir, fileName))
bytes, err := os.ReadFile(filepath.Clean(filepath.Join(tempDir, fileName)))
require.NoError(t, err)
require.Equal(t, "1\n11\n111\n", string(bytes))
}