Skip to content

Commit

Permalink
fix issue 85 (#86)
Browse files Browse the repository at this point in the history
fix issue 85
  • Loading branch information
sashamelentyev authored Oct 22, 2022
1 parent 018e855 commit 20cf01f
Show file tree
Hide file tree
Showing 7 changed files with 125 additions and 61 deletions.
33 changes: 32 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ on:
pull_request:
branches: [ main ]

env:
GO_VERSION: 1.19.2

jobs:
run:
runs-on: ubuntu-latest
Expand All @@ -17,7 +20,7 @@ jobs:
- name: Install Go
uses: actions/setup-go@v3
with:
go-version: 1.19.2
go-version: ${{ env.GO_VERSION }}

- name: Go Format
run: gofmt -s -w . && git diff --exit-code
Expand All @@ -38,3 +41,31 @@ jobs:
run: go install golang.org/x/vuln/cmd/govulncheck@latest
- name: Run govulncheck
run: govulncheck ./...

tests-on-windows:
needs: run
runs-on: windows-latest
steps:
- uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

- name: Go Test
run: go test -v -race ./...

tests-on-macos:
needs: run
runs-on: macos-latest
steps:
- uses: actions/checkout@v3

- name: Install Go
uses: actions/setup-go@v3
with:
go-version: ${{ env.GO_VERSION }}

- name: Go Test
run: go test -v -race ./...
46 changes: 22 additions & 24 deletions pkg/analyzer/analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,37 +8,35 @@ import (
"github.com/sashamelentyev/usestdlibvars/pkg/analyzer"
)

var flags = []string{
analyzer.TimeWeekdayFlag,
analyzer.TimeMonthFlag,
analyzer.TimeLayoutFlag,
analyzer.CryptoHashFlag,
analyzer.RPCDefaultPathFlag,
analyzer.SQLIsolationLevelFlag,
analyzer.TLSSignatureSchemeFlag,
analyzer.ConstantKindFlag,
}

var pkgs = []string{
"a/crypto",
"a/http",
"a/rpc",
"a/time",
"a/sql",
"a/tls",
"a/constant",
}

func TestUseStdlibVars(t *testing.T) {
a := analyzer.New()

for _, flag := range []string{
analyzer.TimeWeekdayFlag,
analyzer.TimeMonthFlag,
analyzer.TimeLayoutFlag,
analyzer.CryptoHashFlag,
analyzer.RPCDefaultPathFlag,
analyzer.OSDevNullFlag,
analyzer.SQLIsolationLevelFlag,
analyzer.TLSSignatureSchemeFlag,
analyzer.ConstantKindFlag,
analyzer.SyslogPriorityFlag,
} {
for _, flag := range flags {
if err := a.Flags.Set(flag, "true"); err != nil {
t.Fatal(err)
}
}

pkgs := []string{
"a/crypto",
"a/http",
"a/rpc",
"a/time",
"a/os",
"a/sql",
"a/tls",
"a/constant",
"a/syslog",
}

analysistest.Run(t, analysistest.TestData(), a, pkgs...)
}
38 changes: 2 additions & 36 deletions pkg/analyzer/internal/mapping/mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ import (
"crypto/tls"
"database/sql"
"go/constant"
"log/syslog"
"net/http"
"net/rpc"
"os"
"strconv"
"time"
)
Expand Down Expand Up @@ -165,9 +163,7 @@ var TimeLayout = map[string]string{
time.StampNano: "time.StampNano",
}

var OSDevNull = map[string]string{
os.DevNull: "os.DevNull",
}
var OSDevNull = map[string]string{}

var SQLIsolationLevel = map[string]string{
// sql.LevelDefault.String(): "sql.LevelDefault.String()",
Expand Down Expand Up @@ -204,34 +200,4 @@ var ConstantKind = map[string]string{
constant.Complex.String(): "constant.Complex.String()",
}

var SyslogPriority = map[string]string{
// strconv.Itoa(int(syslog.LOG_EMERG)): "syslog.LOG_EMERG",
strconv.Itoa(int(syslog.LOG_ALERT)): "syslog.LOG_ALERT",
strconv.Itoa(int(syslog.LOG_CRIT)): "syslog.LOG_CRIT",
strconv.Itoa(int(syslog.LOG_ERR)): "syslog.LOG_ERR",
strconv.Itoa(int(syslog.LOG_WARNING)): "syslog.LOG_WARNING",
strconv.Itoa(int(syslog.LOG_NOTICE)): "syslog.LOG_NOTICE",
strconv.Itoa(int(syslog.LOG_INFO)): "syslog.LOG_INFO",
strconv.Itoa(int(syslog.LOG_DEBUG)): "syslog.LOG_DEBUG",

// strconv.Itoa(int(syslog.LOG_KERN)): "syslog.LOG_KERN",
strconv.Itoa(int(syslog.LOG_USER)): "syslog.LOG_USER",
strconv.Itoa(int(syslog.LOG_MAIL)): "syslog.LOG_MAIL",
strconv.Itoa(int(syslog.LOG_DAEMON)): "syslog.LOG_DAEMON",
strconv.Itoa(int(syslog.LOG_AUTH)): "syslog.LOG_AUTH",
strconv.Itoa(int(syslog.LOG_SYSLOG)): "syslog.LOG_SYSLOG",
strconv.Itoa(int(syslog.LOG_LPR)): "syslog.LOG_LPR",
strconv.Itoa(int(syslog.LOG_NEWS)): "syslog.LOG_NEWS",
strconv.Itoa(int(syslog.LOG_UUCP)): "syslog.LOG_UUCP",
strconv.Itoa(int(syslog.LOG_CRON)): "syslog.LOG_CRON",
strconv.Itoa(int(syslog.LOG_AUTHPRIV)): "syslog.LOG_AUTHPRIV",
strconv.Itoa(int(syslog.LOG_FTP)): "syslog.LOG_FTP",
strconv.Itoa(int(syslog.LOG_LOCAL0)): "syslog.LOG_LOCAL0",
strconv.Itoa(int(syslog.LOG_LOCAL1)): "syslog.LOG_LOCAL1",
strconv.Itoa(int(syslog.LOG_LOCAL2)): "syslog.LOG_LOCAL2",
strconv.Itoa(int(syslog.LOG_LOCAL3)): "syslog.LOG_LOCAL3",
strconv.Itoa(int(syslog.LOG_LOCAL4)): "syslog.LOG_LOCAL4",
strconv.Itoa(int(syslog.LOG_LOCAL5)): "syslog.LOG_LOCAL5",
strconv.Itoa(int(syslog.LOG_LOCAL6)): "syslog.LOG_LOCAL6",
strconv.Itoa(int(syslog.LOG_LOCAL7)): "syslog.LOG_LOCAL7",
}
var SyslogPriority = map[string]string{}
9 changes: 9 additions & 0 deletions pkg/analyzer/internal/mapping/osdevnull.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//go:build unix || (js && wasm)

package mapping

import "os"

func init() {
OSDevNull[os.DevNull] = "os.DevNull"
}
40 changes: 40 additions & 0 deletions pkg/analyzer/internal/mapping/syslogpriority.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//go:build !windows && !plan9

package mapping

import (
"log/syslog"
"strconv"
)

func init() {
// SyslogPriority[strconv.Itoa(int(syslog.LOG_EMERG))] = "syslog.LOG_EMERG"
SyslogPriority[strconv.Itoa(int(syslog.LOG_ALERT))] = "syslog.LOG_ALERT"
SyslogPriority[strconv.Itoa(int(syslog.LOG_CRIT))] = "syslog.LOG_CRIT"
SyslogPriority[strconv.Itoa(int(syslog.LOG_ERR))] = "syslog.LOG_ERR"
SyslogPriority[strconv.Itoa(int(syslog.LOG_WARNING))] = "syslog.LOG_WARNING"
SyslogPriority[strconv.Itoa(int(syslog.LOG_NOTICE))] = "syslog.LOG_NOTICE"
SyslogPriority[strconv.Itoa(int(syslog.LOG_INFO))] = "syslog.LOG_INFO"
SyslogPriority[strconv.Itoa(int(syslog.LOG_DEBUG))] = "syslog.LOG_DEBUG"

// SyslogPriority[strconv.Itoa(int(syslog.LOG_KERN))] = "syslog.LOG_KERN"
SyslogPriority[strconv.Itoa(int(syslog.LOG_USER))] = "syslog.LOG_USER"
SyslogPriority[strconv.Itoa(int(syslog.LOG_MAIL))] = "syslog.LOG_MAIL"
SyslogPriority[strconv.Itoa(int(syslog.LOG_DAEMON))] = "syslog.LOG_DAEMON"
SyslogPriority[strconv.Itoa(int(syslog.LOG_AUTH))] = "syslog.LOG_AUTH"
SyslogPriority[strconv.Itoa(int(syslog.LOG_SYSLOG))] = "syslog.LOG_SYSLOG"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LPR))] = "syslog.LOG_LPR"
SyslogPriority[strconv.Itoa(int(syslog.LOG_NEWS))] = "syslog.LOG_NEWS"
SyslogPriority[strconv.Itoa(int(syslog.LOG_UUCP))] = "syslog.LOG_UUCP"
SyslogPriority[strconv.Itoa(int(syslog.LOG_CRON))] = "syslog.LOG_CRON"
SyslogPriority[strconv.Itoa(int(syslog.LOG_AUTHPRIV))] = "syslog.LOG_AUTHPRIV"
SyslogPriority[strconv.Itoa(int(syslog.LOG_FTP))] = "syslog.LOG_FTP"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL0))] = "syslog.LOG_LOCAL0"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL1))] = "syslog.LOG_LOCAL1"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL2))] = "syslog.LOG_LOCAL2"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL3))] = "syslog.LOG_LOCAL3"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL4))] = "syslog.LOG_LOCAL4"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL5))] = "syslog.LOG_LOCAL5"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL6))] = "syslog.LOG_LOCAL6"
SyslogPriority[strconv.Itoa(int(syslog.LOG_LOCAL7))] = "syslog.LOG_LOCAL7"
}
10 changes: 10 additions & 0 deletions pkg/analyzer/os_dev_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build unix || (js && wasm)

package analyzer_test

import "github.com/sashamelentyev/usestdlibvars/pkg/analyzer"

func init() {
flags = append(flags, analyzer.OSDevNullFlag)
pkgs = append(pkgs, "a/os")
}
10 changes: 10 additions & 0 deletions pkg/analyzer/syslog_priority_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//go:build !windows && !plan9

package analyzer_test

import "github.com/sashamelentyev/usestdlibvars/pkg/analyzer"

func init() {
flags = append(flags, analyzer.SyslogPriorityFlag)
pkgs = append(pkgs, "a/syslog")
}

0 comments on commit 20cf01f

Please sign in to comment.