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

Enforce import order #254

Merged
merged 4 commits into from
Jul 31, 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
14 changes: 14 additions & 0 deletions .github/.golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
linters:
disable-all: true
enable:
- gci
linters-settings:
gci:
sections:
- standard
- default
- Prefix(github.com/canonical/pebble)
issues:
# these values ensure that all issues will be surfaced
max-issues-per-linter: 0
max-same-issues: 0
30 changes: 30 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Lint
on: [push, pull_request]

jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/setup-go@v4
with:
go-mod-file: 'go.mod'
cache: false

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
barrettj12 marked this conversation as resolved.
Show resolved Hide resolved
id: lint
with:
version: latest
args: '-c .github/.golangci.yml --out-format=colored-line-number'
skip-cache: true

- name: Print error message
if: always() && steps.lint.outcome == 'failure'
run: |
echo '
Linting failed. On your local machine, please run
golangci-lint run -c .github/.golangci.yml --fix
and check in the changes.'
exit 1
33 changes: 33 additions & 0 deletions HACKING.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,39 @@ $ curl --unix-socket ~/pebble/.pebble.socket 'http://localhost/v1/services?names
```


## Code style
barrettj12 marked this conversation as resolved.
Show resolved Hide resolved

Pebble imports should be arranged in three groups:
- standard library imports
- third-party / non-Pebble imports
- Pebble imports (i.e. those prefixed with `github.com/canonical/pebble`)

Imports should be sorted alphabetically within each group.

We use the [`gopkg.in/check.v1`](https://pkg.go.dev/gopkg.in/check.v1) package for testing. Inside a test file, import this as follows:
```go
. "gopkg.in/check.v1"
```
so that identifiers from that package will be added to the local namespace.


Here is an example of correctly arranged imports:

```go
import (
"fmt"
"net"
"os"

"github.com/gorilla/mux"
. "gopkg.in/check.v1"

"github.com/canonical/pebble/internals/systemd"
"github.com/canonical/pebble/internals/testutil"
)
```


## Running the tests

Pebble has a suite of Go unit tests, which you can run using the regular `go test` command. To test all packages in the Pebble repository:
Expand Down
1 change: 0 additions & 1 deletion internals/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"unicode/utf8"

"github.com/canonical/go-flags"

"golang.org/x/crypto/ssh/terminal"

"github.com/canonical/pebble/client"
Expand Down
1 change: 0 additions & 1 deletion internals/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"testing"

"golang.org/x/crypto/ssh/terminal"

. "gopkg.in/check.v1"

"github.com/canonical/pebble/cmd"
Expand Down
3 changes: 0 additions & 3 deletions internals/cli/format_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ import (
"os"
"runtime"

// "fmt"
// "net/http"

"gopkg.in/check.v1"

"github.com/canonical/pebble/internals/cli"
Expand Down
4 changes: 2 additions & 2 deletions internals/daemon/api_changes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"net/http/httptest"
"time"

"github.com/canonical/pebble/internals/overlord/state"

"gopkg.in/check.v1"

"github.com/canonical/pebble/internals/overlord/state"
)

func setupChanges(st *state.State) []string {
Expand Down
3 changes: 2 additions & 1 deletion internals/daemon/api_files_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ import (
"syscall"
"time"

. "gopkg.in/check.v1"

"github.com/canonical/pebble/internals/osutil"
"github.com/canonical/pebble/internals/osutil/sys"
. "gopkg.in/check.v1"
)

var _ = Suite(&filesSuite{})
Expand Down
4 changes: 2 additions & 2 deletions internals/daemon/api_services_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ import (
"strings"
"time"

"github.com/canonical/pebble/internals/overlord/state"

. "gopkg.in/check.v1"

"github.com/canonical/pebble/internals/overlord/state"
)

var servicesLayer = `
Expand Down
4 changes: 2 additions & 2 deletions internals/daemon/api_warnings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"net/url"
"time"

"github.com/canonical/pebble/internals/overlord/state"

"gopkg.in/check.v1"

"github.com/canonical/pebble/internals/overlord/state"
)

func (s *apiSuite) testWarnings(c *check.C, all bool, body io.Reader) (calls string, result interface{}) {
Expand Down
3 changes: 1 addition & 2 deletions internals/daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ import (
"syscall"
"time"

"gopkg.in/tomb.v2"

"github.com/gorilla/mux"
"gopkg.in/tomb.v2"

"github.com/canonical/pebble/internals/logger"
"github.com/canonical/pebble/internals/osutil"
Expand Down
Loading