Skip to content
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: 18 additions & 0 deletions e2e/framework/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,24 @@ func CopyToTempDir(relativePath string) (string, error) {
return CopyToTempDirInDir("", relativePath)
}

// WriteTempDevcontainer writes content to devcontainer.json inside a new temp dir named by prefix and returns the dir.
// The caller owns cleanup of the returned dir.
func WriteTempDevcontainer(prefix, content string) string {
tempDir, err := os.MkdirTemp("", prefix)
ExpectNoError(err)

devcontainerDir := filepath.Join(tempDir, ".devcontainer")
ExpectNoError(os.MkdirAll(devcontainerDir, 0o750))
ExpectNoError(
os.WriteFile(
filepath.Join(devcontainerDir, "devcontainer.json"),
[]byte(content),
0o600,
),
)
return tempDir
}

func CleanupTempDir(initialDir, tempDir string) {
err := os.Chdir(initialDir)
ExpectNoError(err)
Expand Down
23 changes: 3 additions & 20 deletions e2e/tests/outdated/outdated.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net/http/httptest"
"os"
"path/filepath"
"strings"

"github.com/devsy-org/devsy/e2e/framework"
Expand Down Expand Up @@ -42,7 +41,7 @@ var _ = ginkgo.Describe("outdated command", ginkgo.Label("outdated"), func() {
framework.PushFeatureTag(featureRepo + ":1.1.0")
framework.PushFeatureTag(featureRepo + ":2.0.0")

tempDir := writeDevcontainerJSON(fmt.Sprintf(`{
tempDir := framework.WriteTempDevcontainer("outdated-test-*", fmt.Sprintf(`{
"image": "ubuntu:22.04",
"features": {"%s:1.0.0": {}}
}`, featureRepo))
Expand Down Expand Up @@ -72,7 +71,7 @@ var _ = ginkgo.Describe("outdated command", ginkgo.Label("outdated"), func() {
framework.PushFeatureTag(featureRepo + ":1.0.0")
framework.PushFeatureTag(featureRepo + ":1.1.0")

tempDir := writeDevcontainerJSON(fmt.Sprintf(`{
tempDir := framework.WriteTempDevcontainer("outdated-test-*", fmt.Sprintf(`{
"image": "ubuntu:22.04",
"features": {"%s:1.1.0": {}}
}`, featureRepo))
Expand All @@ -94,7 +93,7 @@ var _ = ginkgo.Describe("outdated command", ginkgo.Label("outdated"), func() {
func(ctx context.Context) {
f := framework.NewDefaultFramework(initialDir + "/bin")

tempDir := writeDevcontainerJSON(`{
tempDir := framework.WriteTempDevcontainer("outdated-test-*", `{
"image": "ubuntu:22.04"
}`)
ginkgo.DeferCleanup(func() { _ = os.RemoveAll(tempDir) })
Expand All @@ -110,19 +109,3 @@ var _ = ginkgo.Describe("outdated command", ginkgo.Label("outdated"), func() {
ginkgo.SpecTimeout(framework.TimeoutShort()),
)
})

func writeDevcontainerJSON(content string) string {
tempDir, err := os.MkdirTemp("", "outdated-test-*")
framework.ExpectNoError(err)

devcontainerDir := filepath.Join(tempDir, ".devcontainer")
framework.ExpectNoError(os.MkdirAll(devcontainerDir, 0o750))
framework.ExpectNoError(
os.WriteFile(
filepath.Join(devcontainerDir, "devcontainer.json"),
[]byte(content),
0o600,
),
)
return tempDir
}
27 changes: 5 additions & 22 deletions e2e/tests/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ var _ = ginkgo.Describe("upgrade command", ginkgo.Label("upgrade"), func() {
framework.PushFeatureTag(featureRepo + ":1.1.0")
framework.PushFeatureTag(featureRepo + ":2.0.0")

tempDir := writeDevcontainerJSON(fmt.Sprintf(`{
tempDir := framework.WriteTempDevcontainer("upgrade-test-*", fmt.Sprintf(`{
"image": "ubuntu:22.04",
"features": {"%s:1.0.0": {}}
}`, featureRepo))
Expand Down Expand Up @@ -82,7 +82,7 @@ var _ = ginkgo.Describe("upgrade command", ginkgo.Label("upgrade"), func() {
"image": "ubuntu:22.04",
"features": {"%s:1.0.0": {}}
}`, featureRepo)
tempDir := writeDevcontainerJSON(originalContent)
tempDir := framework.WriteTempDevcontainer("upgrade-test-*", originalContent)
ginkgo.DeferCleanup(func() { _ = os.RemoveAll(tempDir) })

stdout, _, err := f.ExecCommandCapture(ctx, []string{
Expand Down Expand Up @@ -119,7 +119,7 @@ var _ = ginkgo.Describe("upgrade command", ginkgo.Label("upgrade"), func() {
framework.PushFeatureTag(featureB + ":1.0.0")
framework.PushFeatureTag(featureB + ":2.0.0")

tempDir := writeDevcontainerJSON(fmt.Sprintf(`{
tempDir := framework.WriteTempDevcontainer("upgrade-test-*", fmt.Sprintf(`{
"image": "ubuntu:22.04",
"features": {
"%s:1.0.0": {},
Expand Down Expand Up @@ -159,7 +159,7 @@ var _ = ginkgo.Describe("upgrade command", ginkgo.Label("upgrade"), func() {

framework.PushFeatureTag(featureRepo + ":1.0.0")

tempDir := writeDevcontainerJSON(fmt.Sprintf(`{
tempDir := framework.WriteTempDevcontainer("upgrade-test-*", fmt.Sprintf(`{
"image": "ubuntu:22.04",
"features": {"%s:1.0.0": {}}
}`, featureRepo))
Expand Down Expand Up @@ -192,7 +192,7 @@ var _ = ginkgo.Describe("upgrade command", ginkgo.Label("upgrade"), func() {
framework.PushFeatureTag(featureRepo + ":1.0.0")
framework.PushFeatureTag(featureRepo + ":2.0.0")

tempDir := writeDevcontainerJSON(fmt.Sprintf(`{
tempDir := framework.WriteTempDevcontainer("upgrade-test-*", fmt.Sprintf(`{
// Important comment
"image": "ubuntu:22.04",
"features": {"%s:1.0.0": {}}
Expand All @@ -215,23 +215,6 @@ var _ = ginkgo.Describe("upgrade command", ginkgo.Label("upgrade"), func() {
)
})

func writeDevcontainerJSON(content string) string {
tempDir, err := os.MkdirTemp("", "upgrade-test-*")
framework.ExpectNoError(err)

devcontainerDir := filepath.Join(tempDir, ".devcontainer")
framework.ExpectNoError(os.MkdirAll(devcontainerDir, 0o750))
framework.ExpectNoError(
os.WriteFile(
filepath.Join(devcontainerDir, "devcontainer.json"),
[]byte(content),
0o600,
),
)

return tempDir
}

func readDevcontainerJSON(tempDir string) string {
// #nosec G304 -- path constructed from test temp directory
data, err := os.ReadFile(
Expand Down
Loading