Skip to content
This repository has been archived by the owner on Nov 13, 2023. It is now read-only.

Commit

Permalink
Merge pull request #18 from gha-trigger/ci/improve-test
Browse files Browse the repository at this point in the history
ci: improve test
  • Loading branch information
suzuki-shunsuke authored Sep 23, 2022
2 parents 86dd509 + 354d473 commit aaf338a
Show file tree
Hide file tree
Showing 9 changed files with 240 additions and 181 deletions.
204 changes: 23 additions & 181 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,194 +7,36 @@ on:
branches: [main]

jobs:
test-pull-request:
list-files:
runs-on: ubuntu-latest
outputs:
files: ${{env.FILES}}
steps:
- uses: actions/checkout@v3
- uses: ./
- uses: actions/setup-go@v3
with:
data: |
{
"event_name": "pull_request",
"event": {
"number": 149,
"sender": {
"login": "suzuki-shunsuke"
},
"repository": {
"full_name": "gha-trigger/gha-trigger",
"name": "gha-trigger",
"owner": {
"login": "gha-trigger"
}
},
"pull_request": {
"base": {
"ref": "main"
},
"head": {
"sha": "b6d8f3331c6845997b9408163a127e01fd32c120",
"ref": "renovate/github.com-aws-aws-sdk-go-1.x"
},
"html_url": "https://github.com/gha-trigger/step-summary-action/pull/9"
}
},
"pull_request": {
"merge_commit_sha": "4528ea7ab7a8eef7e596097934a78eab90fb8b33",
"base": {
"ref": "main"
},
"head": {
"sha": "b6d8f3331c6845997b9408163a127e01fd32c120",
"ref": "renovate/github.com-aws-aws-sdk-go-1.x"
},
"html_url": "https://github.com/gha-trigger/step-summary-action/pull/9"
}
}
- name: Test GHA_ACTOR
run: |
[ "$GHA_ACTOR" = suzuki-shunsuke ]
- name: Test GHA_EVENT_NAME
run: |
[ "$GHA_EVENT_NAME" = pull_request ]
- name: Test GHA_REPOSITORY
run: |
[ "$GHA_REPOSITORY" = gha-trigger/gha-trigger ]
- name: Test GHA_REPOSITORY_OWNER
run: |
[ "$GHA_REPOSITORY_OWNER" = gha-trigger ]
- name: Test GHA_REPOSITORY_NAME
run: |
[ "$GHA_REPOSITORY_NAME" = gha-trigger ]
- name: Test GHA_SHA
run: |
[ "$GHA_SHA" = 4528ea7ab7a8eef7e596097934a78eab90fb8b33 ]
- name: Test GHA_COMMIT_STATUS_SHA
run: |
[ "$GHA_COMMIT_STATUS_SHA" = b6d8f3331c6845997b9408163a127e01fd32c120 ]
- name: Test GHA_BASE_REF
run: |
[ "$GHA_BASE_REF" = main ]
- name: Test GHA_HEAD_SHA
run: |
[ "$GHA_HEAD_SHA" = b6d8f3331c6845997b9408163a127e01fd32c120 ]
- name: Test GHA_REF
run: |
[ "$GHA_REF" = refs/pull/149/merge ]
- name: Test GHA_REF_NAME
run: |
[ "$GHA_REF_NAME" = 149/merge ]
- name: Test GHA_PULL_REQUEST_NUMBER
run: |
[ "$GHA_PULL_REQUEST_NUMBER" = 149 ]
- name: Test GHA_ENV
run: |
[ -n "$GHA_ENV" ]
test-push:
go-version: "1.19.1"
cache: true
- run: go run ./cmd/list

test:
runs-on: ubuntu-latest
name: "test (${{matrix.file}})"
needs: list-files
env:
FILE: ${{matrix.file}}
strategy:
fail-fast: false
matrix:
file: ${{fromJSON(needs.list-files.outputs.files)}}
steps:
- uses: actions/checkout@v3
- uses: ./
- uses: actions/setup-go@v3
with:
data: |
{
"event_name": "push",
"event": {
"ref": "refs/heads/renovate/github.com-aws-aws-sdk-go-1.x",
"sender": {
"login": "suzuki-shunsuke"
},
"head_commit": {
"id": "a705bd6d8bc3882c983477cb3f20ab74ef8c24c2"
},
"repository": {
"full_name": "gha-trigger/gha-trigger",
"name": "gha-trigger",
"owner": {
"login": "gha-trigger"
}
}
}
}
- name: Test GHA_ACTOR
run: |
[ "$GHA_ACTOR" = suzuki-shunsuke ]
- name: Test GHA_EVENT_NAME
run: |
[ "$GHA_EVENT_NAME" = push ]
- name: Test GHA_REPOSITORY
run: |
[ "$GHA_REPOSITORY" = gha-trigger/gha-trigger ]
- name: Test GHA_REPOSITORY_OWNER
run: |
[ "$GHA_REPOSITORY_OWNER" = gha-trigger ]
- name: Test GHA_REPOSITORY_NAME
run: |
[ "$GHA_REPOSITORY_NAME" = gha-trigger ]
- name: Test GHA_SHA
run: |
[ "$GHA_SHA" = a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 ]
- name: Test GHA_COMMIT_STATUS_SHA
run: |
[ "$GHA_COMMIT_STATUS_SHA" = a705bd6d8bc3882c983477cb3f20ab74ef8c24c2 ]
- name: Test GHA_REF
run: |
[ "$GHA_REF" = refs/heads/renovate/github.com-aws-aws-sdk-go-1.x ]
- name: Test GHA_REF_NAME
run: |
[ "$GHA_REF_NAME" = renovate/github.com-aws-aws-sdk-go-1.x ]
- name: Test GHA_ENV
run: |
[ -n "$GHA_ENV" ]
test-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
go-version: "1.19.1"
cache: true
- run: go run ./cmd/setter "$FILE"
- uses: ./
with:
data: |
{
"event_name": "release",
"event": {
"sender": {
"login": "suzuki-shunsuke"
},
"release": {
"tag_name": "v0.1.0"
},
"repository": {
"full_name": "gha-trigger/gha-trigger",
"name": "gha-trigger",
"owner": {
"login": "gha-trigger"
}
}
}
}
- name: Test GHA_ACTOR
run: |
[ "$GHA_ACTOR" = suzuki-shunsuke ]
- name: Test GHA_EVENT_NAME
run: |
[ "$GHA_EVENT_NAME" = release ]
- name: Test GHA_REPOSITORY
run: |
[ "$GHA_REPOSITORY" = gha-trigger/gha-trigger ]
- name: Test GHA_REPOSITORY_OWNER
run: |
[ "$GHA_REPOSITORY_OWNER" = gha-trigger ]
- name: Test GHA_REPOSITORY_NAME
run: |
[ "$GHA_REPOSITORY_NAME" = gha-trigger ]
- name: Test GHA_SHA
run: |
[ "$GHA_SHA" = "" ]
- name: Test GHA_REF
run: |
[ "$GHA_REF" = v0.1.0 ]
- name: Test GHA_REF_NAME
run: |
[ "$GHA_REF_NAME" = v0.1.0 ]
- name: Test GHA_ENV
run: |
[ -n "$GHA_ENV" ]
data: ${{toJSON(fromJSON(env.DATA).data)}}
- run: go run ./cmd/tester "$FILE"
29 changes: 29 additions & 0 deletions cmd/list/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"encoding/json"
"log"
"path/filepath"

"github.com/sethvargo/go-githubactions"
)

func main() {
if err := core(); err != nil {
log.Fatal(err)
}
}

func core() error {
files, err := filepath.Glob("testdata/*/*")
if err != nil {
return err
}
b, err := json.Marshal(files)
if err != nil {
return err
}

githubactions.SetEnv("FILES", string(b))
return nil
}
44 changes: 44 additions & 0 deletions cmd/setter/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"encoding/json"
"log"
"os"

"github.com/sethvargo/go-githubactions"
"gopkg.in/yaml.v3"
)

func main() {
if err := core(); err != nil {
log.Fatal(err)
}
}

type Config struct {
Envs map[string]string
Data interface{}
}

func core() error {
// read config file
// test envs
file := os.Args[1]
cfg := &Config{}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
return err
}

b, err := json.Marshal(cfg)
if err != nil {
return err
}

githubactions.SetEnv("DATA", string(b))
return nil
}
47 changes: 47 additions & 0 deletions cmd/tester/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package main

import (
"errors"
"log"
"os"

"gopkg.in/yaml.v3"
)

func main() {
if err := core(); err != nil {
log.Fatal(err)
}
}

type Config struct {
Envs map[string]string
Data interface{}
}

func core() error {
// read config file
// test envs
file := os.Args[1]
cfg := &Config{}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
if err := yaml.NewDecoder(f).Decode(cfg); err != nil {
return err
}
failed := false
for envName, expValue := range cfg.Envs {
actValue := os.Getenv(envName)
if expValue != actValue {
log.Printf("[ERROR] the environment variable %s is wrong: wanted %s, got %s", envName, expValue, actValue)
failed = true
}
}
if failed {
return errors.New("[ERROR] Some environment variables are wrong")
}
return nil
}
10 changes: 10 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module github.com/gha-trigger/set-env-action

go 1.19

require (
github.com/sethvargo/go-githubactions v1.0.0
gopkg.in/yaml.v3 v3.0.1
)

require github.com/sethvargo/go-envconfig v0.6.0 // indirect
9 changes: 9 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
github.com/google/go-cmp v0.4.1 h1:/exdXoGamhu5ONeUJH0deniYLWYvQwW66yvlfiiKTu0=
github.com/sethvargo/go-envconfig v0.6.0 h1:GxxdoeiNpWgGiVEphNFNObgMYRN/ZvI2dN7rBwadyss=
github.com/sethvargo/go-envconfig v0.6.0/go.mod h1:00S1FAhRUuTNJazWBWcJGvEHOM+NO6DhoRMAOX7FY5o=
github.com/sethvargo/go-githubactions v1.0.0 h1:5mYGPNxIwIXaS8MLj4uYGWM8QM8giUVqA4FuSYOZjXE=
github.com/sethvargo/go-githubactions v1.0.0/go.mod h1:UaidDD1ENTLXzTtj/4MnYjY40/5WLijgn2O8KBsdv7o=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
31 changes: 31 additions & 0 deletions testdata/pull_request/normal.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
envs:
GHA_ACTOR: suzuki-shunsuke
GHA_EVENT_NAME: pull_request
GHA_REPOSITORY: gha-trigger/gha-trigger
GHA_REPOSITORY_OWNER: gha-trigger
GHA_REPOSITORY_NAME: gha-trigger
GHA_SHA: 4528ea7ab7a8eef7e596097934a78eab90fb8b33
GHA_COMMIT_STATUS_SHA: b6d8f3331c6845997b9408163a127e01fd32c120
GHA_BASE_REF: main
GHA_HEAD_SHA: b6d8f3331c6845997b9408163a127e01fd32c120
GHA_REF: refs/pull/149/merge
GHA_REF_NAME: 149/merge
GHA_PULL_REQUEST_NUMBER: "149"
data:
event_name: pull_request
event:
number: 149
sender:
login: suzuki-shunsuke
repository:
full_name: gha-trigger/gha-trigger
name: gha-trigger
owner:
login: gha-trigger
pull_request:
merge_commit_sha: 4528ea7ab7a8eef7e596097934a78eab90fb8b33
base:
ref: main
head:
sha: b6d8f3331c6845997b9408163a127e01fd32c120
ref: renovate/github.com-aws-aws-sdk-go-1.x
Loading

0 comments on commit aaf338a

Please sign in to comment.