Skip to content

Add ci linter #1817

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

Merged
merged 2 commits into from
Nov 13, 2020
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
31 changes: 4 additions & 27 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ commands:
executors:
build-env:
environment:
## Split key to avoid github revoking it
password0: '99544cdcb19ad4e3fd64'
password1: '3ec86b2e5a431be2d72c'
GLOG_logtostderr: '1'
## Split key to avoid github revoking it
password0: "99544cdcb19ad4e3fd64"
password1: "3ec86b2e5a431be2d72c"
GLOG_logtostderr: "1"
docker:
- image: docker.pkg.github.com/grpc-ecosystem/grpc-gateway/build-env:1.15
auth:
Expand Down Expand Up @@ -79,27 +79,6 @@ jobs:
- checkout
- generate
- run: git diff --exit-code
lint:
executor: build-env
working_directory: /src/grpc-gateway
steps:
- checkout
- restore_cache:
keys:
- v1-staticcheck-cache-{{ checksum "go.sum" }}
- v1-staticcheck-cache-
- run:
name: Install staticcheck outside local module
command: |
cd $(mktemp -d) &&
go mod init tmp &&
go get honnef.co/go/tools/cmd/staticcheck
- run: staticcheck ./...
- save_cache:
key: v1-staticcheck-cache-{{ checksum "go.sum" }}
paths:
- /root/.cache/go-build
- /root/.cache/staticcheck
fuzzit:
docker:
- image: fuzzitdev/fuzzit:golang1.12-stretch-llvm9
Expand Down Expand Up @@ -190,7 +169,6 @@ workflows:
- fuzzit
- node_test
- generate
- lint
- bazel
- gorelease
- release:
Expand All @@ -215,4 +193,3 @@ workflows:
only: /renovate\/master-.+/
tags:
ignore: /.*/

19 changes: 19 additions & 0 deletions .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: golangci-lint
on:
push:
tags:
- v*
branches:
- master
pull_request:
jobs:
golangci:
name: lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.32
args: --enable goimports
298 changes: 149 additions & 149 deletions internal/descriptor/grpc_api_configuration_test.go
Original file line number Diff line number Diff line change
@@ -1,149 +1,149 @@
package descriptor
import (
"strings"
"testing"
)
func TestLoadGrpcAPIServiceFromYAMLInvalidType(t *testing.T) {
// Ideally this would fail but for now this test documents that it doesn't
service, err := loadGrpcAPIServiceFromYAML([]byte(`type: not.the.right.type`), "invalidtype")
if err != nil {
t.Fatal(err)
}
if service == nil {
t.Fatal("No service returned")
}
}
func TestLoadGrpcAPIServiceFromYAMLSingleRule(t *testing.T) {
service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3
http:
rules:
- selector: grpctest.YourService.Echo
post: /v1/myecho
body: "*"
`), "example")
if err != nil {
t.Fatal(err)
}
if service.Http == nil {
t.Fatal("HTTP is empty")
}
if len(service.Http.GetRules()) != 1 {
t.Fatalf("Have %v rules instead of one. Got: %v", len(service.Http.GetRules()), service.Http.GetRules())
}
rule := service.Http.GetRules()[0]
if rule.GetSelector() != "grpctest.YourService.Echo" {
t.Errorf("Rule has unexpected selector '%v'", rule.GetSelector())
}
if rule.GetPost() != "/v1/myecho" {
t.Errorf("Rule has unexpected post '%v'", rule.GetPost())
}
if rule.GetBody() != "*" {
t.Errorf("Rule has unexpected body '%v'", rule.GetBody())
}
}
func TestLoadGrpcAPIServiceFromYAMLRejectInvalidYAML(t *testing.T) {
service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3
http:
rules:
- selector: grpctest.YourService.Echo
- post: thislinebreakstheselectorblockabovewiththeleadingdash
body: "*"
`), "invalidyaml")
if err == nil {
t.Fatal(err)
}
if !strings.Contains(err.Error(), "line 7") {
t.Errorf("Expected yaml error to be detected in line 7. Got other error: %v", err)
}
if service != nil {
t.Fatal("Service returned")
}
}
func TestLoadGrpcAPIServiceFromYAMLMultipleWithAdditionalBindings(t *testing.T) {
service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3
http:
rules:
- selector: first.selector
post: /my/post/path
body: "*"
additional_bindings:
- post: /additional/post/path
- put: /additional/put/{value}/path
- delete: "{value}"
- patch: "/additional/patch/{value}"
- selector: some.other.service
delete: foo
`), "example")
if err != nil {
t.Fatalf("Failed to load service description from YAML: %v", err)
}
if service == nil {
t.Fatal("No service returned")
}
if service.Http == nil {
t.Fatal("HTTP is empty")
}
if len(service.Http.GetRules()) != 2 {
t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
}
first := service.Http.GetRules()[0]
if first.GetSelector() != "first.selector" {
t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
}
if first.GetBody() != "*" {
t.Errorf("first.selector has unexpected body '%v'", first.GetBody())
}
if first.GetPost() != "/my/post/path" {
t.Errorf("first.selector has unexpected post '%v'", first.GetPost())
}
if len(first.GetAdditionalBindings()) != 4 {
t.Fatalf("first.selector has unexpected number of bindings %v instead of four. Got: %v", len(first.GetAdditionalBindings()), first.GetAdditionalBindings())
}
if first.GetAdditionalBindings()[0].GetPost() != "/additional/post/path" {
t.Errorf("first.selector additional binding 0 has unexpected post '%v'", first.GetAdditionalBindings()[0].GetPost())
}
if first.GetAdditionalBindings()[1].GetPut() != "/additional/put/{value}/path" {
t.Errorf("first.selector additional binding 1 has unexpected put '%v'", first.GetAdditionalBindings()[0].GetPost())
}
if first.GetAdditionalBindings()[2].GetDelete() != "{value}" {
t.Errorf("first.selector additional binding 2 has unexpected delete '%v'", first.GetAdditionalBindings()[0].GetPost())
}
if first.GetAdditionalBindings()[3].GetPatch() != "/additional/patch/{value}" {
t.Errorf("first.selector additional binding 3 has unexpected patch '%v'", first.GetAdditionalBindings()[0].GetPost())
}
second := service.Http.GetRules()[1]
if second.GetSelector() != "some.other.service" {
t.Errorf("some.other.service has unexpected selector '%v'", second.GetSelector())
}
if second.GetDelete() != "foo" {
t.Errorf("some.other.service has unexpected delete '%v'", second.GetDelete())
}
if len(second.GetAdditionalBindings()) != 0 {
t.Errorf("some.other.service has %v additional bindings when it should not have any. Got: %v", len(second.GetAdditionalBindings()), second.GetAdditionalBindings())
}
}
package descriptor

import (
"strings"
"testing"
)

func TestLoadGrpcAPIServiceFromYAMLInvalidType(t *testing.T) {
// Ideally this would fail but for now this test documents that it doesn't
service, err := loadGrpcAPIServiceFromYAML([]byte(`type: not.the.right.type`), "invalidtype")
if err != nil {
t.Fatal(err)
}

if service == nil {
t.Fatal("No service returned")
}
}

func TestLoadGrpcAPIServiceFromYAMLSingleRule(t *testing.T) {
service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

http:
rules:
- selector: grpctest.YourService.Echo
post: /v1/myecho
body: "*"
`), "example")
if err != nil {
t.Fatal(err)
}

if service.Http == nil {
t.Fatal("HTTP is empty")
}

if len(service.Http.GetRules()) != 1 {
t.Fatalf("Have %v rules instead of one. Got: %v", len(service.Http.GetRules()), service.Http.GetRules())
}

rule := service.Http.GetRules()[0]
if rule.GetSelector() != "grpctest.YourService.Echo" {
t.Errorf("Rule has unexpected selector '%v'", rule.GetSelector())
}
if rule.GetPost() != "/v1/myecho" {
t.Errorf("Rule has unexpected post '%v'", rule.GetPost())
}
if rule.GetBody() != "*" {
t.Errorf("Rule has unexpected body '%v'", rule.GetBody())
}
}

func TestLoadGrpcAPIServiceFromYAMLRejectInvalidYAML(t *testing.T) {
service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

http:
rules:
- selector: grpctest.YourService.Echo
- post: thislinebreakstheselectorblockabovewiththeleadingdash
body: "*"
`), "invalidyaml")
if err == nil {
t.Fatal(err)
}

if !strings.Contains(err.Error(), "line 7") {
t.Errorf("Expected yaml error to be detected in line 7. Got other error: %v", err)
}

if service != nil {
t.Fatal("Service returned")
}
}

func TestLoadGrpcAPIServiceFromYAMLMultipleWithAdditionalBindings(t *testing.T) {
service, err := loadGrpcAPIServiceFromYAML([]byte(`
type: google.api.Service
config_version: 3

http:
rules:
- selector: first.selector
post: /my/post/path
body: "*"
additional_bindings:
- post: /additional/post/path
- put: /additional/put/{value}/path
- delete: "{value}"
- patch: "/additional/patch/{value}"
- selector: some.other.service
delete: foo
`), "example")
if err != nil {
t.Fatalf("Failed to load service description from YAML: %v", err)
}

if service == nil {
t.Fatal("No service returned")
}

if service.Http == nil {
t.Fatal("HTTP is empty")
}

if len(service.Http.GetRules()) != 2 {
t.Fatalf("%v service(s) returned when two were expected. Got: %v", len(service.Http.GetRules()), service.Http)
}

first := service.Http.GetRules()[0]
if first.GetSelector() != "first.selector" {
t.Errorf("first.selector has unexpected selector '%v'", first.GetSelector())
}
if first.GetBody() != "*" {
t.Errorf("first.selector has unexpected body '%v'", first.GetBody())
}
if first.GetPost() != "/my/post/path" {
t.Errorf("first.selector has unexpected post '%v'", first.GetPost())
}
if len(first.GetAdditionalBindings()) != 4 {
t.Fatalf("first.selector has unexpected number of bindings %v instead of four. Got: %v", len(first.GetAdditionalBindings()), first.GetAdditionalBindings())
}
if first.GetAdditionalBindings()[0].GetPost() != "/additional/post/path" {
t.Errorf("first.selector additional binding 0 has unexpected post '%v'", first.GetAdditionalBindings()[0].GetPost())
}
if first.GetAdditionalBindings()[1].GetPut() != "/additional/put/{value}/path" {
t.Errorf("first.selector additional binding 1 has unexpected put '%v'", first.GetAdditionalBindings()[0].GetPost())
}
if first.GetAdditionalBindings()[2].GetDelete() != "{value}" {
t.Errorf("first.selector additional binding 2 has unexpected delete '%v'", first.GetAdditionalBindings()[0].GetPost())
}
if first.GetAdditionalBindings()[3].GetPatch() != "/additional/patch/{value}" {
t.Errorf("first.selector additional binding 3 has unexpected patch '%v'", first.GetAdditionalBindings()[0].GetPost())
}

second := service.Http.GetRules()[1]
if second.GetSelector() != "some.other.service" {
t.Errorf("some.other.service has unexpected selector '%v'", second.GetSelector())
}
if second.GetDelete() != "foo" {
t.Errorf("some.other.service has unexpected delete '%v'", second.GetDelete())
}
if len(second.GetAdditionalBindings()) != 0 {
t.Errorf("some.other.service has %v additional bindings when it should not have any. Got: %v", len(second.GetAdditionalBindings()), second.GetAdditionalBindings())
}
}
Loading