Skip to content

Commit

Permalink
Merge pull request #27 from mumoshu/fix-imports-not-importing-sub-jobs
Browse files Browse the repository at this point in the history
Fix import/imports not importing non-top-level jobs via shebang
  • Loading branch information
mumoshu committed Sep 23, 2020
2 parents 64a0e8b + 41b6a1b commit 97a4b02
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 14 deletions.
21 changes: 18 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: Go
on: [push]
jobs:
build:
test:
runs-on: ubuntu-latest
strategy:
matrix:
Expand All @@ -16,8 +16,6 @@ jobs:
version: ${{ matrix.go }}
- name: Run go mod download
run: go mod download
- name: Run golangci-lint
run: make lint
- name: Install SSH key
uses: shimataro/ssh-key-action@v2
with:
Expand All @@ -29,3 +27,20 @@ jobs:
sudo apt-get update -y
sudo apt-get install ruby -y
make test smoke
lint:
runs-on: ubuntu-latest
strategy:
matrix:
go:
- 1.13.4
name: Go ${{ matrix.go }} build
steps:
- uses: actions/checkout@master
- name: Setup Go
uses: actions/setup-go@v1
with:
version: ${{ matrix.go }}
- name: Run go mod download
run: go mod download
- name: Run golangci-lint
run: make lint
3 changes: 3 additions & 0 deletions examples/advanced/import/mycli
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env variant

import = "."
2 changes: 1 addition & 1 deletion examples/module/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FROM alpine:3.10

ARG HELM_VERSION=3.0.0
ARG HELM_VERSION=3.2.0
ARG HELM_FILENAME="helm-${HELM_VERSION}-linux-amd64.tar.gz"

ADD http://storage.googleapis.com/kubernetes-helm/${HELM_FILE_NAME} /tmp
Expand Down
2 changes: 1 addition & 1 deletion examples/module/default.variantmod
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module "default" {
dependency "github_release" "helm" {
source = "helm/helm"
version = "> 1.0.0, < 3.0.1"
version = ">= 3.0.0, < 3.2.1"
}

file "Dockerfile" {
Expand Down
4 changes: 2 additions & 2 deletions examples/module/module_test.variant
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
test "test" {
case "ok" {
out = trimspace(<<EOS
version.BuildInfo{Version:"v3.0.0", GitCommit:"e29ce2a54e96cd02ccfce88bee4f58bb6e2a28b6", GitTreeState:"clean", GoVersion:"go1.13.4"}
version.BuildInfo{Version:"v3.2.0", GitCommit:"e11b7ce3b12db2941e90399e874513fbd24bcb71", GitTreeState:"clean", GoVersion:"go1.13.10"}
EOS
)
exitstatus = 0
Expand All @@ -25,7 +25,7 @@ test "build" {
out = trimspace(<<EOS
FROM alpine:3.10

ARG HELM_VERSION=3.0.0
ARG HELM_VERSION=3.2.0
ARG HELM_FILENAME="helm-$${HELM_VERSION}-linux-amd64.tar.gz"

ADD http://storage.googleapis.com/kubernetes-helm/$${HELM_FILE_NAME} /tmp
Expand Down
53 changes: 46 additions & 7 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"strings"
"testing"

"golang.org/x/sync/errgroup"

"github.com/google/go-cmp/cmp"
)

Expand Down Expand Up @@ -139,6 +141,13 @@ func TestExamples(t *testing.T) {
args: []string{"variant", "test"},
wd: "./examples/advanced/import",
},
{
subject: "import/shebang",
variantName: "",
args: []string{"variant", "./examples/advanced/import/mycli", "foo", "bar", "HELLO"},
wd: "./examples/advanced/import",
expectOut: "HELLO\n",
},
{
subject: "import-multi",
variantName: "",
Expand Down Expand Up @@ -209,10 +218,15 @@ func TestExamples(t *testing.T) {
},
}

for i := range testcases {
for idx := range testcases {
i := idx
tc := testcases[i]

t.Run(fmt.Sprintf("%d: %s", i, tc.subject), func(t *testing.T) {
t.Logf("Running subtest: %d %s", i, tc.subject)

outRead, outWrite := io.Pipe()
errRead, errWrite := io.Pipe()
env := Env{
Args: tc.args,
Getenv: func(name string) string {
Expand All @@ -237,27 +251,52 @@ func TestExamples(t *testing.T) {
go func() {
err = RunMain(env, func(m *Main) {
m.Stdout = outWrite
m.Stderr = os.Stderr
m.Stderr = errWrite
m.Getenv = env.Getenv
m.Getwd = env.Getwd
})
outWrite.Close()
errWrite.Close()
}()

buf := new(bytes.Buffer)
if _, err := buf.ReadFrom(outRead); err != nil {
var out, errOut string

eg := &errgroup.Group{}

eg.Go(func() error {
outBuf := new(bytes.Buffer)
if _, err := outBuf.ReadFrom(outRead); err != nil {
return fmt.Errorf("reading stdout: %w", err)
}

out = outBuf.String()

return nil
})

eg.Go(func() error {
errBuf := new(bytes.Buffer)
if _, err := errBuf.ReadFrom(errRead); err != nil {
return fmt.Errorf("reading stderr: %w", err)
}

errOut = errBuf.String()

return nil
})

if err := eg.Wait(); err != nil {
t.Fatalf("unexpected error: %v", err)
}
out := buf.String()

if tc.expectErr != "" {
if err == nil {
t.Fatalf("Expected error didn't occur")
} else if err.Error() != tc.expectErr {
t.Fatalf("Unexpected error: want %q, got %q", tc.expectErr, err.Error())
t.Fatalf("Unexpected error: want %q, got %q\n%s", tc.expectErr, err.Error(), errOut)
}
} else if err != nil {
t.Fatalf("%+v", err)
t.Fatalf("%+v\n%s", err, errOut)
}

if tc.expectOut != "" {
Expand Down
10 changes: 10 additions & 0 deletions pkg/app/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,8 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap
// their types MUST match.
merged := mergeJobs(importedJob, j)

merged.Name = ""

importedJob = *merged
}

Expand Down Expand Up @@ -322,6 +324,14 @@ func newApp(app *App, cc *HCL2Config, importDir func(string) (*App, error)) (*Ap

app.JobByName = jobByName

var newJobs []JobSpec

for _, j := range app.JobByName {
newJobs = append(newJobs, j)
}

app.Config.Jobs = newJobs

return app, nil
}

Expand Down

0 comments on commit 97a4b02

Please sign in to comment.