Skip to content

Commit

Permalink
Merge pull request #45 from giusdp/main
Browse files Browse the repository at this point in the history
Add lint check CI job
  • Loading branch information
francescotimperi authored May 5, 2023
2 parents c8fe47c + 6d3015b commit 3c2217d
Show file tree
Hide file tree
Showing 13 changed files with 96 additions and 51 deletions.
18 changes: 17 additions & 1 deletion .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,20 @@ jobs:
run: |
rm -Rf ~/.nuv/olaris
bats/bin/bats .
linter:
name: Lint
runs-on: ubuntu-latest
steps:
- name: Checkout recursive
uses: actions/checkout@v3
with:
submodules: recursive
- uses: actions/setup-go@v4
with:
go-version: '>=1.20.2'
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: latest
args: --timeout=3m
2 changes: 1 addition & 1 deletion auth/login_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func setupMockServer(t *testing.T, expectedLogin, expectedPass, expectedRes stri
t.Errorf("expected password %s, got %s", expectedPass, password)
}

w.Write([]byte(expectedRes))
_, _ = w.Write([]byte(expectedRes))
}))

return server
Expand Down
11 changes: 9 additions & 2 deletions common.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ func readNuvRootFile(dir string) (NuvRootJSON, error) {
if err != nil {
return NuvRootJSON{}, err
}
json.Unmarshal(json_buf, &data)
if err := json.Unmarshal(json_buf, &data); err != nil {
warn("nuvroot.json parsed with an error", err)
}
return data, nil
}

Expand All @@ -113,7 +115,12 @@ func readNuvConfigFile(dir string) (map[string]interface{}, error) {
if err != nil {
return nil, err
}
json.Unmarshal(json_buf, &data)
if err := json.Unmarshal(json_buf, &data); err != nil {
if data == nil {
return nil, err
}
warn("config.json parsed with an error", err)
}
return data, nil
}

Expand Down
4 changes: 2 additions & 2 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ func createTestNuvRootFile(t *testing.T, dir string, onlyVersion bool) {
}
nuvRootStr = fmt.Sprintf("{ \"version\": \"0.3.0\", \"config\": %s }", configJson)
}
os.WriteFile(path, []byte(nuvRootStr), 0644)
_ = os.WriteFile(path, []byte(nuvRootStr), 0644)
}

func createTestConfigJson(t *testing.T, dir string, envVars map[string]interface{}) {
Expand All @@ -639,7 +639,7 @@ func createTestConfigJson(t *testing.T, dir string, envVars map[string]interface
if err != nil {
t.Errorf("error: %v", err)
}
os.WriteFile(filepath.Join(dir, "config.json"), []byte(configJson), 0644)
_ = os.WriteFile(filepath.Join(dir, "config.json"), []byte(configJson), 0644)
}

func Test_mergeMaps(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ func pr(args ...any) {
}

// as creates a string array
func as(s ...string) []string {
return s
}
// func as(s ...string) []string {
// return s
// }

var homeDir = ""
var workDir = ""
Expand Down
7 changes: 4 additions & 3 deletions nuv.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"bufio"
"fmt"
"io/ioutil"
"os"
"regexp"
"sort"
Expand Down Expand Up @@ -115,7 +114,7 @@ func setupTmp() {
// load saved args in files names _*_ in current directory
func loadSavedArgs() []string {
res := []string{}
files, err := ioutil.ReadDir(".")
files, err := os.ReadDir(".")
if err != nil {
return res
}
Expand Down Expand Up @@ -174,7 +173,9 @@ func Nuv(base string, args []string) error {
}
// if valid, check if it's a folder and move to it
if isDir(taskName) && exists(taskName, NUVFILE) {
os.Chdir(taskName)
if err := os.Chdir(taskName); err != nil {
return err
}
//remove it from the args
rest = rest[1:]
} else {
Expand Down
20 changes: 10 additions & 10 deletions nuv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import (
"golang.org/x/exp/slices"
)

func ExampleNuvArg() {
func Example_nuvArg() {
// test
os.Chdir(workDir)
_ = os.Chdir(workDir)
olaris, _ := filepath.Abs(joinpath("tests", "olaris"))
err := Nuv(olaris, split("testcmd"))
pr(2, err)
Expand All @@ -52,7 +52,7 @@ func ExampleNuvArg() {

func ExampleNuv() {
// test
os.Chdir(workDir)
_ = os.Chdir(workDir)
olaris, _ := filepath.Abs(joinpath("tests", "olaris"))
err := Nuv(olaris, split(""))
pr(1, err)
Expand Down Expand Up @@ -80,7 +80,7 @@ func ExampleNuv() {
}

func ExampleParseArgs() {
os.Chdir(workDir)
_ = os.Chdir(workDir)
usage := readfile("tests/olaris/sub/opts/nuvopts.txt")
args := parseArgs(usage, split("ciao mike miri max"))
pr(1, args)
Expand Down Expand Up @@ -116,7 +116,7 @@ func Test_validateTaskName(t *testing.T) {

tmpDir := createTmpNuvfile(t, testNuvfile)
defer os.RemoveAll(tmpDir)
os.Chdir(tmpDir)
_ = os.Chdir(tmpDir)
for _, tt := range validateTaskTests {
task, err := validateTaskName(tt.argTask)
if err != nil && !strings.Contains(err.Error(), tt.expected) {
Expand All @@ -129,8 +129,8 @@ func Test_validateTaskName(t *testing.T) {
}
}

func ExampleTmp() {
os.Chdir(workDir)
func Example_setupTmp() {
_ = os.Chdir(workDir)
nuvdir, _ := homedir.Expand("~/.nuv")
os.RemoveAll(nuvdir)
setupTmp()
Expand All @@ -140,10 +140,10 @@ func ExampleTmp() {
// /home/.nuv/tmp
}

func ExampleLoadArgs() {
os.Chdir(workDir)
func Example_loadArgs() {
_ = os.Chdir(workDir)
fmt.Println(1, loadSavedArgs())
os.Chdir(joinpath("tests", "testdata"))
_ = os.Chdir(joinpath("tests", "testdata"))
fmt.Println(2, loadSavedArgs())
// Output:
// 1 []
Expand Down
8 changes: 5 additions & 3 deletions prepare.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ func downloadTasksFromGitHub(force bool, silent bool) (string, error) {
if err != nil {
return "", err
}
os.MkdirAll(nuvDir, 0755)
if err := os.MkdirAll(nuvDir, 0755); err != nil {
return "", err
}
localDir, err := homedir.Expand("~/.nuv/olaris")
if err != nil {
return "", err
Expand Down Expand Up @@ -121,8 +123,8 @@ func pullTasks(force, silent bool) error {

// check if the version is up to date, if not warn the user but continue
if nuvVersion.LessThan(nuvRootVersion) {
warn("Your nuv version", nuvVersion, "is older than the required version in nuvroot.json.")
warn("Please update nuv to the latest version.")
fmt.Printf("Your nuv version %v is older than the required version in nuvroot.json.\n", nuvVersion)
fmt.Println("Please update nuv to the latest version.")
}

return nil
Expand Down
15 changes: 7 additions & 8 deletions prepare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
)

func Example_locate() {
os.Chdir(workDir)
_ = os.Chdir(workDir)
dir, err := locateNuvRoot("tests")
pr(1, err, npath(dir))
dir, err = locateNuvRoot(joinpath("tests", "olaris"))
Expand All @@ -37,21 +37,20 @@ func Example_locate() {
}

func Example_locate_git() {
os.Chdir(workDir)
_ = os.Chdir(workDir)
NuvBranch = "test"
nuvdir, _ := homedir.Expand("~/.nuv")
os.RemoveAll(nuvdir)
os.Setenv("NUV_BIN", "")
//_, err :=
_ = os.RemoveAll(nuvdir)
_ = os.Setenv("NUV_BIN", "")
_, err := locateNuvRoot(".")
pr(1, err)
os.Setenv("NUV_BIN", workDir)
_ = os.Setenv("NUV_BIN", workDir)
dir, err := locateNuvRoot("tests")
pr(2, err, npath(dir))
downloadTasksFromGitHub(true, true)
_, _ = downloadTasksFromGitHub(true, true)
dir, err = locateNuvRoot(".")
pr(3, err, nhpath(dir))
downloadTasksFromGitHub(true, true)
_, _ = downloadTasksFromGitHub(true, true)
dir, err = locateNuvRoot(".")
pr(4, err, nhpath(dir))
os.RemoveAll(nuvdir)
Expand Down
4 changes: 3 additions & 1 deletion serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,9 @@ func nuvTaskServer(w http.ResponseWriter, r *http.Request) {

// write output to response
outjson, _ := json.Marshal(nuvOut)
w.Write(outjson)
if _, err := w.Write(outjson); err != nil {
debug("Failed to write response", err)
}
}

func execCommandTasks(tasks []string) NuvOutput {
Expand Down
4 changes: 2 additions & 2 deletions serve_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
)

func TestIndex(t *testing.T) {
os.Chdir(workDir)
_ = os.Chdir(workDir)
olaris, _ := filepath.Abs(joinpath("tests", "olaris"))
handler := webFileServerHandler(joinpath(olaris, WebDir))
ts := httptest.NewServer(handler)
Expand Down Expand Up @@ -65,7 +65,7 @@ func TestIndex(t *testing.T) {
}

func TestGetTask(t *testing.T) {
os.Chdir(joinpath(workDir, "tests"))
_ = os.Chdir(joinpath(workDir, "tests"))

t.Run("/api/nuv?test invokes test task", func(t *testing.T) {
request := newTaskRequest("test")
Expand Down
28 changes: 19 additions & 9 deletions updates_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,28 +92,38 @@ func touchLatestCheckFile(latest_check_path string) {
}

func checkRemoteOlarisNewer(olaris_path string) bool {
isRemoteNewer := false

repo, err := git.PlainOpen(olaris_path)
if err != nil {
warn("failed to check olaris folder", err)
return isRemoteNewer
return false
}

localRef, _ := repo.Head()
localRef, err := repo.Head()
if err != nil {
warn("failed to check olaris folder", err)
return false
}

remote, _ := repo.Remote("origin")
remote.Fetch(&git.FetchOptions{})
remoteRefs, _ := remote.List(&git.ListOptions{})
remote, err := repo.Remote("origin")
if err != nil {
warn("failed to check remote olaris", err)
return false
}
_ = remote.Fetch(&git.FetchOptions{})
remoteRefs, err := remote.List(&git.ListOptions{})
if err != nil {
warn("failed to check remote olaris", err)
return false
}

// check ref is in refs
for _, remoteRef := range remoteRefs {
if localRef.Name().String() == remoteRef.Name().String() {
// is hash different?
if localRef.Hash().String() != remoteRef.Hash().String() {
isRemoteNewer = true
return true
}
}
}
return isRemoteNewer
return false
}
20 changes: 14 additions & 6 deletions updates_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,22 @@ func resetOneCommit(repo *git.Repository) {
Order: git.LogOrderCommitterTime,
})

commIter.Next()
secondLastCommit, _ := commIter.Next()
if _, err := commIter.Next(); err != nil {
pr("failed to get first commit", err)
}
secondLastCommit, err := commIter.Next()
if err != nil {
pr("failed to get second last commit", err)
}

w, _ := repo.Worktree()
w.Reset(&git.ResetOptions{
if err := w.Reset(&git.ResetOptions{
Mode: git.HardReset,
Commit: secondLastCommit.Hash,
})
}); err != nil {
pr("failed to reset repo", err)
}

}

func Example_checkUpdated_uptodate() {
Expand All @@ -63,7 +71,7 @@ func Example_checkUpdated_uptodate() {

olarisTmpPath := joinpath(tmpDir, "olaris")

_, err = git.PlainClone(olarisTmpPath, false, &git.CloneOptions{
_, _ = git.PlainClone(olarisTmpPath, false, &git.CloneOptions{
URL: getNuvRepo(),
Progress: os.Stderr},
)
Expand Down Expand Up @@ -97,7 +105,7 @@ func Example_checkUpdated_outdated() {

olarisTmpPath := joinpath(tmpDir, "olaris")

repo, err := git.PlainClone(olarisTmpPath, false, &git.CloneOptions{
repo, _ := git.PlainClone(olarisTmpPath, false, &git.CloneOptions{
URL: getNuvRepo(),
Progress: os.Stderr},
)
Expand Down

0 comments on commit 3c2217d

Please sign in to comment.