Skip to content

Commit

Permalink
Add staticcheck linter (taiidani#23)
Browse files Browse the repository at this point in the history
* Add staticcheck linter

* Update acceptance test

* Also run acceptance tests on master
  • Loading branch information
taiidani committed Dec 13, 2020
1 parent 8134bf4 commit d7a122d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 60 deletions.
67 changes: 35 additions & 32 deletions .github/workflows/acceptance.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,43 @@
name: Acceptance Tests

on: pull_request
on:
pull_request:
push:
branches:
- main
- master

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.14
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- run: go mod download

- name: Set up services
env:
COMPOSE_FILE: ./example/docker-compose.yml
run: |
docker-compose build
docker-compose up -d --force-recreate jenkins
while [ "$(docker inspect jenkins-provider-acc --format '{{ .State.Health.Status }}')" != "healthy" ]; do echo "Waiting for Jenkins to start..."; sleep 3; done
- name: Run Acceptance Tests
env:
TF_ACC: "1"
JENKINS_URL: "http://localhost:8080"
JENKINS_USERNAME: "admin"
JENKINS_PASSWORD: "admin"
run: go test -v -covermode=atomic -coverprofile=coverage.txt ./...

- name: Send coverage to Codecov
run: bash <(curl -s https://codecov.io/bash) -Z -f coverage.txt -F acceptance
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.14
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- run: go mod download

- name: Set up services
env:
COMPOSE_FILE: ./example/docker-compose.yml
run: |
docker-compose build
docker-compose up -d --force-recreate jenkins
while [ "$(docker inspect jenkins-provider-acc --format '{{ .State.Health.Status }}')" != "healthy" ]; do echo "Waiting for Jenkins to start..."; sleep 3; done
- name: Run Acceptance Tests
env:
TF_ACC: "1"
JENKINS_URL: "http://localhost:8080"
JENKINS_USERNAME: "admin"
JENKINS_PASSWORD: "admin"
run: go test -v -covermode=atomic -coverprofile=coverage.txt ./...

- name: Send coverage to Codecov
run: bash <(curl -s https://codecov.io/bash) -Z -f coverage.txt -F acceptance
41 changes: 21 additions & 20 deletions .github/workflows/unit.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
name: Unit Tests

on: [ push ]
on: [push]

jobs:

build:
name: Build
runs-on: ubuntu-latest
steps:

- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.14
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- run: go mod download
- run: go vet ./...

- name: Test
run: go test -race -covermode=atomic -coverprofile=coverage.txt ./...

- name: Send coverage to Codecov
run: bash <(curl -s https://codecov.io/bash) -Z -f coverage.txt -F unit
- name: Set up Go 1.x
uses: actions/setup-go@v2
with:
go-version: ^1.15
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v2

- run: go mod download
- run: go vet ./...
- run: |
go get honnef.co/go/tools/cmd/staticcheck
staticcheck -tests ./...
- name: Test
run: go test -race -covermode=atomic -coverprofile=coverage.txt ./...

- name: Send coverage to Codecov
run: bash <(curl -s https://codecov.io/bash) -Z -f coverage.txt -F unit
4 changes: 2 additions & 2 deletions jenkins/resource_jenkins_credential_username.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func resourceJenkinsCredentialUsernameCreate(ctx context.Context, d *schema.Reso

// Validate that the folder exists
if err := folderExists(client, cm.Folder); err != nil {
return diag.FromErr(fmt.Errorf("Invalid folder name '%s' specified: %w", cm.Folder, err))
return diag.FromErr(fmt.Errorf("invalid folder name '%s' specified: %w", cm.Folder, err))
}

cred := jenkins.UsernameCredentials{
Expand Down Expand Up @@ -176,7 +176,7 @@ func resourceJenkinsCredentialUsernameImport(ctx context.Context, d *schema.Reso

splitID := strings.Split(d.Id(), "/")
if len(splitID) < 2 {
return ret, fmt.Errorf("Import ID was improperly formatted. Imports need to be in the format \"[<folder>/]<domain>/<name>\"")
return ret, fmt.Errorf("import ID was improperly formatted. Imports need to be in the format \"[<folder>/]<domain>/<name>\"")
}

name := splitID[len(splitID)-1]
Expand Down
11 changes: 6 additions & 5 deletions jenkins/resource_jenkins_credential_username_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,33 @@ import (

func TestAccJenkinsCredentialUsername_basic(t *testing.T) {
var cred jenkins.UsernameCredentials
// randString := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckJenkinsCredentialUsernameDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(`
Config: `
resource jenkins_credential_username foo {
name = "test-username"
username = "foo"
password = "bar"
}`),
}`,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("jenkins_credential_username.foo", "id", "/test-username"),
testAccCheckJenkinsCredentialUsernameExists("jenkins_credential_username.foo", &cred),
),
},
{
// Update by adding description
Config: fmt.Sprintf(`
Config: `
resource jenkins_credential_username foo {
name = "test-username"
description = "new-description"
username = "foo"
password = "bar"
}`),
}`,
Check: resource.ComposeTestCheckFunc(
testAccCheckJenkinsCredentialUsernameExists("jenkins_credential_username.foo", &cred),
resource.TestCheckResourceAttr("jenkins_credential_username.foo", "description", "new-description"),
Expand Down Expand Up @@ -161,6 +160,8 @@ func testAccCheckJenkinsCredentialUsernameDestroy(s *terraform.State) error {
for _, rs := range s.RootModule().Resources {
if rs.Type != "jenkins_credential_username" {
continue
} else if _, ok := rs.Primary.Meta["name"]; !ok {
continue
}

cred := jenkins.UsernameCredentials{}
Expand Down
3 changes: 3 additions & 0 deletions jenkins/resource_jenkins_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func resourceJenkinsJobUpdate(ctx context.Context, d *schema.ResourceData, meta

// grab job by current name
job, err := client.GetJob(name, folders...)
if err != nil {
return diag.FromErr(fmt.Errorf("jenkins::update - Could not find job %q: %w", name, err))
}

xml, err := renderTemplate(d.Get("template").(string), d)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion jenkins/validations.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func validateJobName(val interface{}, path cty.Path) diag.Diagnostics {
if strings.Contains(val.(string), "/") {
return diag.FromErr(fmt.Errorf("Provided name includes path characters. Please use the 'folder' property if specifying a job within a subfolder"))
return diag.FromErr(fmt.Errorf("provided name includes path characters. Please use the 'folder' property if specifying a job within a subfolder"))
}

return diag.Diagnostics{}
Expand Down

0 comments on commit d7a122d

Please sign in to comment.