Skip to content

Tidy up codebase to deal with new linter errors #171

Description

The following came from github copilot's analysis of #166:

The failing job encountered several linter errors. The most critical are due to error return values from file Close operations not being checked in your Go code (errcheck), a malformed test name (govet), capitalization of error strings (staticcheck), and a couple of staticcheck suggestions. Here’s how you can fix them:


1. Check Error Returns for File Close Operations

In cmd/foreach/foreach.go, you should check errors when deferring .Close() on files. Change:

defer successfulReposFile.Close()
defer failedReposFile.Close()

to:

defer func() {
    if err := successfulReposFile.Close(); err != nil {
        logger.Warnf("Failed to close successfulReposFile: %v", err)
    }
}()
defer func() {
    if err := failedReposFile.Close(); err != nil {
        logger.Warnf("Failed to close failedReposFile: %v", err)
    }
}()

Apply the same pattern for:

defer reposFile.Close()
defer logs.Close()

Replace all simple defer file.Close() with a defer func(){ ... }() as above.


2. Test Files: Check Error Returns for os.RemoveAll

In cmd/foreach/foreach_test.go, update:

defer os.RemoveAll("mock_output")

to:

defer func() {
    if err := os.RemoveAll("mock_output"); err != nil {
        t.Logf("Failed to remove mock_output: %v", err)
    }
}()

Do this for all similar occurrences.


3. Test Name Capitalization (govet)

In internal/github/util_test.go, rename:

func TestuserHasPushPermissionReturnsErrorForInvalidJSON(t *testing.T)

to:

func TestUserHasPushPermissionReturnsErrorForInvalidJSON(t *testing.T)

4. Error String Capitalization (staticcheck)

In cmd/foreach/foreach.go:

return errors.New("Use -- to separate command")

should be:

return errors.New("use -- to separate command")

5. Prefer fmt.Fprintf Over WriteString(fmt.Sprintf(...)) (staticcheck)

Instead of:

_, _ = successfulReposFile.WriteString(fmt.Sprintf("# This file contains ..."))

Use:

fmt.Fprintf(successfulReposFile, "# This file contains ...")

Do the same for failedReposFile.


6. Deprecation Warning for io/ioutil (staticcheck)

In cmd/init/init_test.go, replace:

import "io/ioutil"
...
readmeContents, err := ioutil.ReadFile("foo/README.md")

with:

import "os"
...
readmeContents, err := os.ReadFile("foo/README.md")

Summary

  • Check error returns for all file .Close() and os.RemoveAll() operations.
  • Fix test function naming to start with Test.
  • Use lowercase for the start of error messages.
  • Replace WriteString(fmt.Sprintf(...)) with fmt.Fprintf.
  • Use os.ReadFile instead of ioutil.ReadFile.

Apply these changes and rerun your linter/job. These fixes should resolve all the linter errors shown in the logs. If you need the specific code snippets for any file, let me know!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions