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

Add Makefile targets to lint and fix errors #23

Merged
merged 1 commit into from
Apr 5, 2016
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
18 changes: 15 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2015 The OPA Authors. All rights reserved.
# Copyright 2016 The OPA Authors. All rights reserved.
# Use of this source code is governed by an Apache2
# license that can be found in the LICENSE file.

Expand All @@ -18,13 +18,14 @@ GO := go
GO15VENDOREXPERIMENT := 1
export GO15VENDOREXPERIMENT

.PHONY: all deps generate build test clean
.PHONY: all deps generate build test check check-fmt check-vet check-lint clean

all: build test
all: build test check

deps:
$(GO) install ./vendor/github.com/PuerkitoBio/pigeon
$(GO) install ./vendor/golang.org/x/tools/cmd/goimports
$(GO) install ./vendor/github.com/golang/lint/golint

generate:
$(GO) generate
Expand All @@ -35,5 +36,16 @@ build: generate
test: generate
$(GO) test -v $(PACKAGES)

check: check-fmt check-vet check-lint

check-fmt:
./build/check-fmt.sh

check-vet:
./build/check-vet.sh

check-lint:
./build/check-lint.sh

clean:
rm -f ./opa
20 changes: 20 additions & 0 deletions build/check-fmt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env bash

OPA_DIR=$(dirname "${BASH_SOURCE}")/..
source $OPA_DIR/build/utils.sh

function opa::check_fmt() {
exec 5>&1
exit_code=0
for pkg in $(opa::go_packages); do
for file in $(opa::go_files_in_package $pkg); do
__diff=$(gofmt -d $file | tee >(cat - >&5))
if [ ! -z "$__diff" ]; then
exit_code=1
fi
done
done
exit $exit_code
}

opa::check_fmt
24 changes: 24 additions & 0 deletions build/check-lint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env bash

OPA_DIR=$(
dir=$(dirname "${BASH_SOURCE}")/..
cd "$dir"
pwd
)
source $OPA_DIR/build/utils.sh


function opa::check_lint() {
exec 5>&1
exit_code=0
for pkg in $(opa::go_packages); do
echo $pkg
__output=$(golint $pkg | tee >(cat - >&5))
if [ ! -z "$__output" ]; then
exit_code=1
fi
done
exit $exit_code
}

opa::check_lint
23 changes: 23 additions & 0 deletions build/check-vet.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env bash

OPA_DIR=$(
dir=$(dirname "${BASH_SOURCE}")/..
cd "$dir"
pwd
)
source $OPA_DIR/build/utils.sh

function opa::check_vet() {
exec 5>&1
rc=0
exit_code=0
for pkg in $(opa::go_packages); do
go vet $pkg || rc=$?
if [[ $rc != 0 ]]; then
exit_code=1
fi
done
exit $exit_code
}

opa::check_vet
21 changes: 21 additions & 0 deletions build/utils.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash

set -o errexit
set -o pipefail
set -o nounset

function opa::go_packages() {
for pkg in $(go list ./.../ 2>/dev/null | grep -v vendor); do
echo $pkg
done
}

function opa::go_files_in_package() {
dir=$(go list -f '{{ .Dir }}' $1)
for file in $(go list -f '{{ join .GoFiles "\n" }}' $1); do
echo $dir/$file
done
for file in $(go list -f '{{ join .TestGoFiles "\n" }}' $1); do
echo $dir/$file
done
}
1 change: 1 addition & 0 deletions cmd/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import "github.com/spf13/cobra"
import "path"
import "os"

// RootCommand is the base CLI command that all subcommands are added to.
var RootCommand = &cobra.Command{
Use: path.Base(os.Args[0]),
Short: "Open Policy Agent (OPA)",
Expand Down
16 changes: 11 additions & 5 deletions docs/DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ Requirements:

After cloning the repository, run `make deps` to install the parser generator ("pigeon") into your workspace.

Next, run `make all` to build the project and execute all of the tests. If
this succeeds, there should be a new binary in the top level directory ("opa").
Next, run `make all` to build the project, execute all of the tests, and run
static analysis checks against the code. If this succeeds, there should be a
new binary in the top level directory ("opa").

Verify the build was successful by running `opa version`.

You can re-build the project with `make build` and execute all of the tests
with `make test`.

The static analysis checks (i.e., `go fmt`, `golint`, and `go vet` can be run
with `make check`).

## Workflow

1. Go to [https://github.com/open-policy-agent/opa](https://github.com/open-policy-agent/opa) and fork the repository
Expand Down Expand Up @@ -57,7 +61,9 @@ with `make test`.

1. Develop your changes and regularly update your local branch against upstream.

- Make sure you run `go fmt` on your code before submitting a Pull Request.
- Be sure to run `make check` before submitting your pull request. You
may need to run `go fmt` on your code to make it comply with standard Go
style.

1. Commit changes and push to your fork.

Expand Down Expand Up @@ -86,7 +92,7 @@ contained in the vendor directory.

If you need to add a dependency to the project:

1. Run `glide get <package>` to download the package.
1. Run `glide get <package> --update-vendored` to download the package.
- This command should be used instead of `go get <package>`.
- The package will be stored under the vendor directory.
- The glide.yaml file will be updated.
Expand All @@ -102,4 +108,4 @@ If you need to update the dependencies:

## Opalog

If you need to modify the Opalog syntax you must update opalog/opalog.peg. Both `make build` and `make test` will re-generate the parser but if you want to test the parser generation explicitly you can run `make generate`.
If you need to modify the Opalog syntax you must update opalog/opalog.peg. Both `make build` and `make test` will re-generate the parser but if you want to test the parser generation explicitly you can run `make generate`.
8 changes: 5 additions & 3 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions glide.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package: github.com/open-policy-agent/opa
import:
- package: github.com/PuerkitoBio/pigeon
- package: golang.org/x/tools
subpackages:
- cmd/goimports
- package: github.com/spf13/cobra
- package: github.com/PuerkitoBio/pigeon
- package: golang.org/x/tools
subpackages:
- cmd/goimports
- package: github.com/spf13/cobra
- package: github.com/golang/lint
4 changes: 2 additions & 2 deletions opalog/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ func TestArrayWithVars(t *testing.T) {
}

func TestEmptyComposites(t *testing.T) {
assertParseOneTerm(t, "empty object", "{}", ObjectTerm())
assertParseOneTerm(t, "emtpy array", "[]", ArrayTerm())
assertParseOneTerm(t, "empty object", "{}", ObjectTerm())
assertParseOneTerm(t, "emtpy array", "[]", ArrayTerm())
}

func TestNestedComposites(t *testing.T) {
Expand Down
Loading