Skip to content
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

refactor!: StdOut,StdErr -> Stdout,Stderr #20

Merged
merged 1 commit into from
Jan 12, 2023
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
22 changes: 11 additions & 11 deletions command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,33 +139,33 @@ func RunWithoutSuccessfulExit(o *option.Option, args ...string) *gexec.Session {
return New(o, args...).WithoutSuccessfulExit().Run()
}

// StdOut invokes Run and returns the stdout.
func StdOut(o *option.Option, args ...string) []byte {
// Stdout invokes Run and returns the stdout.
func Stdout(o *option.Option, args ...string) []byte {
return Run(o, args...).Out.Contents()
}

// StdoutStr invokes Run and returns the output in string format.
func StdoutStr(o *option.Option, args ...string) string {
return strings.TrimSpace(string(StdOut(o, args...)))
return strings.TrimSpace(string(Stdout(o, args...)))
}

// StdOutAsLines invokes Run and returns the stdout as lines.
func StdOutAsLines(o *option.Option, args ...string) []string {
// StdoutAsLines invokes Run and returns the stdout as lines.
func StdoutAsLines(o *option.Option, args ...string) []string {
return toLines(Run(o, args...).Out)
}

// StdErr invokes Run and returns the stderr.
func StdErr(o *option.Option, args ...string) []byte {
// Stderr invokes Run and returns the stderr.
func Stderr(o *option.Option, args ...string) []byte {
return Run(o, args...).Err.Contents()
}

// StdErrAsLines invokes Run and returns the stderr as lines.
func StdErrAsLines(o *option.Option, args ...string) []string {
// StderrAsLines invokes Run and returns the stderr as lines.
func StderrAsLines(o *option.Option, args ...string) []string {
return toLines(Run(o, args...).Err)
}

// StdErrStr invokes Run and returns the output in string format.
func StdErrStr(o *option.Option, args ...string) string {
// StderrStr invokes Run and returns the output in string format.
func StderrStr(o *option.Option, args ...string) string {
return string(Run(o, args...).Err.Contents())
}

Expand Down
10 changes: 5 additions & 5 deletions command/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ import (

// GetAllContainerIDs returns all container IDs.
func GetAllContainerIDs(o *option.Option) []string {
return StdOutAsLines(o, "ps", "--all", "--quiet", "--no-trunc")
return StdoutAsLines(o, "ps", "--all", "--quiet", "--no-trunc")
}

// GetAllImageNames returns all image names.
func GetAllImageNames(o *option.Option) []string {
return StdOutAsLines(o, "images", "--all", "--format", "{{.Repository}}:{{.Tag}}")
return StdoutAsLines(o, "images", "--all", "--format", "{{.Repository}}:{{.Tag}}")
}

// GetAllVolumeNames returns all volume names.
func GetAllVolumeNames(o *option.Option) []string {
return StdOutAsLines(o, "volume", "ls", "--quiet")
return StdoutAsLines(o, "volume", "ls", "--quiet")
}

// GetAllNetworkNames returns all network names.
func GetAllNetworkNames(o *option.Option) []string {
return StdOutAsLines(o, "network", "ls", "--format", "{{.Name}}")
return StdoutAsLines(o, "network", "ls", "--format", "{{.Name}}")
}

// GetAllImageIDs returns all image IDs.
func GetAllImageIDs(o *option.Option) []string {
return StdOutAsLines(o, "images", "--all", "--quiet")
return StdoutAsLines(o, "images", "--all", "--quiet")
}
10 changes: 5 additions & 5 deletions tests/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Build(o *option.Option) {
for _, file := range []string{"-f", "--file"} {
file := file
ginkgo.It(fmt.Sprintf("build an image with %s option", file), func() {
stdErr := command.StdErr(o, "build", "--no-cache", file, dockerFilePath, buildContext)
stdErr := command.Stderr(o, "build", "--no-cache", file, dockerFilePath, buildContext)
gomega.Expect(stdErr).Should(gomega.ContainSubstring("built from AnotherDockerfile"))
})
}
Expand All @@ -73,7 +73,7 @@ func Build(o *option.Option) {
secretFile := filepath.Join(buildContext, "secret.txt")
ffs.WriteFile(secretFile, "somesecret")
secret := fmt.Sprintf("id=mysecret,src=%s", secretFile)
stdErr := command.StdErr(o, "build", "--progress=plain", "--no-cache", "-f", dockerFilePath, "--secret", secret, buildContext)
stdErr := command.Stderr(o, "build", "--progress=plain", "--no-cache", "-f", dockerFilePath, "--secret", secret, buildContext)
gomega.Expect(stdErr).Should(gomega.ContainSubstring("somesecret"))
})

Expand All @@ -85,7 +85,7 @@ func Build(o *option.Option) {
`, defaultImage, defaultImage)
dockerFilePath := filepath.Join(buildContext, "Dockerfile.with-target")
ffs.WriteFile(dockerFilePath, containerWithTarget)
stdEr := command.StdErr(o, "build", "--progress=plain", "--no-cache",
stdEr := command.Stderr(o, "build", "--progress=plain", "--no-cache",
"-f", dockerFilePath, "--target", "build_env", buildContext)
gomega.Expect(stdEr).Should(gomega.ContainSubstring("output from build_env"))
gomega.Expect(stdEr).ShouldNot(gomega.ContainSubstring("output from prod_env"))
Expand All @@ -101,7 +101,7 @@ func Build(o *option.Option) {
containerWithBuildArg := "ARG VERSION=latest \n FROM public.ecr.aws/docker/library/alpine:${VERSION}"
dockerFilePath := filepath.Join(buildContext, "Dockerfile.with-build-arg")
ffs.WriteFile(dockerFilePath, containerWithBuildArg)
stdErr := command.StdErr(o, "build", "-f", dockerFilePath, "--no-cache", "--progress=plain",
stdErr := command.Stderr(o, "build", "-f", dockerFilePath, "--no-cache", "--progress=plain",
"--build-arg", "VERSION=3.13", buildContext)
gomega.Expect(stdErr).Should(gomega.ContainSubstring("public.ecr.aws/docker/library/alpine:3.13"))
})
Expand All @@ -112,7 +112,7 @@ func Build(o *option.Option) {
`, defaultImage)
dockerFilePath := filepath.Join(buildContext, "Dockerfile.progress")
ffs.WriteFile(dockerFilePath, dockerFile)
stdErr := command.StdErr(o, "build", "-f", dockerFilePath, "--no-cache", "--progress=plain", buildContext)
stdErr := command.Stderr(o, "build", "-f", dockerFilePath, "--no-cache", "--progress=plain", buildContext)
gomega.Expect(stdErr).Should(gomega.ContainSubstring("progress flag set:2"))
})

Expand Down
2 changes: 1 addition & 1 deletion tests/compose_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func ComposeBuild(o *option.Option) {
})

ginkgo.It("should output progress in plain text format", func() {
composeBuildOutput := command.StdErrStr(o, "compose", "build", "--progress",
composeBuildOutput := command.StderrStr(o, "compose", "build", "--progress",
"plain", "--no-cache", "--file", composeFilePath)
// The docker file contains following command.
// RUN printf 'should only see the final answer when "--progress" is set to be "plain": %d\n' $(expr 1 + 1)
Expand Down
8 changes: 4 additions & 4 deletions tests/compose_logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ func ComposeLogs(o *option.Option) {
command.RemoveAll(o)
})
ginkgo.It("should show the logs with prefixes", func() {
output := command.StdOutAsLines(o, "compose", "logs", "--file", composeFilePath)
output := command.StdoutAsLines(o, "compose", "logs", "--file", composeFilePath)
// Log format: container_name |log_msg
// example: container1_composelogs |hello from service 1
gomega.Expect(output).Should(gomega.ContainElements(
gomega.HavePrefix(containerNames[0]),
gomega.HavePrefix(containerNames[1])))
})
ginkgo.It("should show the logs without prefixes", func() {
output := command.StdOutAsLines(o, "compose", "logs", "--no-log-prefix", "--file", composeFilePath)
output := command.StdoutAsLines(o, "compose", "logs", "--no-log-prefix", "--file", composeFilePath)
// Log format: log_msg
// example: hello from service 1
gomega.Expect(output).ShouldNot(gomega.ContainElements(
Expand All @@ -58,7 +58,7 @@ func ComposeLogs(o *option.Option) {
gomega.Expect(output).ShouldNot(gomega.ContainSubstring("\x1b[3"))
})
ginkgo.It("should show the last line of the logs", func() {
output := command.StdOutAsLines(o, "compose", "logs", services[0], "--tail", "1", "--file", composeFilePath)
output := command.StdoutAsLines(o, "compose", "logs", services[0], "--tail", "1", "--file", composeFilePath)
gomega.Expect(output).Should(gomega.HaveLen(1))
})

Expand All @@ -67,7 +67,7 @@ func ComposeLogs(o *option.Option) {
ginkgo.It(fmt.Sprintf("should show the logs with timestamp with no prefixes and no color [flag %s]", tFlag), func() {
// Log format: YYYY-MM-DDThh:mm:ss.000000000Z LOG MSG
timestampMatcher := gomega.MatchRegexp("^[0-9]{1,4}-[0-9]{1,2}-[0-9]{1,2}T[0-9]{1,2}:[0-9]{1,2}:[0-9]{1,2}.*")
output := command.StdOutAsLines(o, "compose", "logs", tFlag, "--no-log-prefix", "--no-color", "--file", composeFilePath)
output := command.StdoutAsLines(o, "compose", "logs", tFlag, "--no-log-prefix", "--no-color", "--file", composeFilePath)
gomega.Expect(output).Should(gomega.HaveEach(timestampMatcher))
})
}
Expand Down
2 changes: 1 addition & 1 deletion tests/compose_ps.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func ComposePs(o *option.Option) {
command.RemoveAll(o)
})
ginkgo.It("should list services defined in compose file", func() {
psOutput := command.StdOutAsLines(o, "compose", "ps", "--file", composeFilePath)
psOutput := command.StdoutAsLines(o, "compose", "ps", "--file", composeFilePath)
gomega.Expect(psOutput).Should(gomega.ContainElements(
gomega.ContainSubstring(services[0]),
gomega.ContainSubstring(services[1])))
Expand Down
4 changes: 2 additions & 2 deletions tests/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func Exec(o *option.Option) {
env := env
ginkgo.It(fmt.Sprintf("should set the environment variable with %s flag", env), func() {
const envPair = "ENV=1"
lines := command.StdOutAsLines(o, "exec", env, envPair, testContainerName, "env")
lines := command.StdoutAsLines(o, "exec", env, envPair, testContainerName, "env")
gomega.Expect(lines).Should(gomega.ContainElement(envPair))
})
}
Expand All @@ -80,7 +80,7 @@ func Exec(o *option.Option) {
envPath := ffs.CreateTempFile("env", envPair)
ginkgo.DeferCleanup(os.RemoveAll, filepath.Dir(envPath))

envOutput := command.StdOutAsLines(o, "exec", "--env-file", envPath, testContainerName, "env")
envOutput := command.StdoutAsLines(o, "exec", "--env-file", envPath, testContainerName, "env")
gomega.Expect(envOutput).Should(gomega.ContainElement(envPair))
})

Expand Down
4 changes: 2 additions & 2 deletions tests/image_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ func ImageHistory(o *option.Option) {
for _, quiet := range []string{"-q", "--quiet"} {
quiet := quiet
ginkgo.It(fmt.Sprintf("should only display snapshot ID with %s flag", quiet), func() {
ids := removeMissingID(command.StdOutAsLines(o, "image", "history", quiet, defaultImage))
ids := removeMissingID(command.StdoutAsLines(o, "image", "history", quiet, defaultImage))
gomega.Expect(ids).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexFull)))
})
}

ginkgo.It("should only display snapshot ID with --format flag", func() {
ids := removeMissingID(command.StdOutAsLines(o, "image", "history", defaultImage, "--format", "{{.Snapshot}}"))
ids := removeMissingID(command.StdoutAsLines(o, "image", "history", defaultImage, "--format", "{{.Snapshot}}"))
gomega.Expect(ids).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexFull)))
})

Expand Down
2 changes: 1 addition & 1 deletion tests/image_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func ImageInspect(o *option.Option) {

ginkgo.It("should display multiple image RepoTags with --format flag", func() {
pullImage(o, olderAlpineImage)
lines := command.StdOutAsLines(o, "image", "inspect", defaultImage, olderAlpineImage, "--format", "{{(index .RepoTags 0)}}")
lines := command.StdoutAsLines(o, "image", "inspect", defaultImage, olderAlpineImage, "--format", "{{(index .RepoTags 0)}}")
gomega.Expect(lines).Should(gomega.ConsistOf(defaultImage, olderAlpineImage))
})

Expand Down
12 changes: 6 additions & 6 deletions tests/images.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func Images(o *option.Option) {
})

ginkgo.It("should list all the images in a tabular format", func() {
images := command.StdOutAsLines(o, "images")
images := command.StdoutAsLines(o, "images")
header, images := images[0], images[1:]
gomega.Expect(images).ShouldNot(gomega.BeEmpty())
gomega.Expect(header).Should(gomega.MatchRegexp(
Expand All @@ -36,31 +36,31 @@ func Images(o *option.Option) {
// TODO: add more strict validation using output matcher.
})
ginkgo.It("should list all the images with image names in a tabular format ", func() {
images := command.StdOutAsLines(o, "images", "--names")
images := command.StdoutAsLines(o, "images", "--names")
header, images := images[0], images[1:]
gomega.Expect(images).ShouldNot(gomega.BeEmpty())
gomega.Expect(header).Should(gomega.MatchRegexp("NAME[\t ]+IMAGE ID[\t ]+CREATED[\t ]+PLATFORM[\t ]+SIZE[\t ]+BLOB SIZE"))
gomega.Expect(images).Should(gomega.HaveEach((gomega.MatchRegexp(`^(.+[\t ]+){5}.+$`))))
// TODO: add more strict validation using output matcher.
})
ginkgo.It("should list all the images", func() {
images := command.StdOutAsLines(o, "images", "--format", "{{.Repository}}:{{.Tag}}")
images := command.StdoutAsLines(o, "images", "--format", "{{.Repository}}:{{.Tag}}")
gomega.Expect(images).ShouldNot(gomega.BeEmpty())
gomega.Expect(images).Should(gomega.ContainElements(testImageName))
gomega.Expect(images).Should(gomega.ContainElements(defaultImage))
})
ginkgo.It("should list truncated IMAGE IDs", func() {
images := command.StdOutAsLines(o, "images", "--quiet")
images := command.StdoutAsLines(o, "images", "--quiet")
gomega.Expect(images).ShouldNot(gomega.BeEmpty())
gomega.Expect(images).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexTruncated)))
})
ginkgo.It("should list full IMAGE IDs", func() {
images := command.StdOutAsLines(o, "images", "--quiet", "--no-trunc")
images := command.StdoutAsLines(o, "images", "--quiet", "--no-trunc")
gomega.Expect(images).ShouldNot(gomega.BeEmpty())
gomega.Expect(images).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexFull)))
})
ginkgo.It("should list IMAGE digests", func() {
images := command.StdOutAsLines(o, "images", "--digests", "--format", "{{.Digest}}")
images := command.StdoutAsLines(o, "images", "--digests", "--format", "{{.Digest}}")
gomega.Expect(images).ShouldNot(gomega.BeEmpty())
gomega.Expect(images).Should(gomega.HaveEach(gomega.MatchRegexp(sha256RegexFull)))
})
Expand Down
2 changes: 1 addition & 1 deletion tests/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func Inspect(o *option.Option) {
const oldContainerName = "ctr-old"
command.Run(o, "run", "--name", testContainerName, defaultImage)
command.Run(o, "run", "--name", oldContainerName, olderAlpineImage)
images := command.StdOutAsLines(o, "inspect", "--format", "{{.Image}}", testContainerName, oldContainerName)
images := command.StdoutAsLines(o, "inspect", "--format", "{{.Image}}", testContainerName, oldContainerName)
gomega.Expect(images).Should(gomega.ConsistOf(defaultImage, olderAlpineImage))
})

Expand Down
2 changes: 1 addition & 1 deletion tests/network_inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func NetworkInspect(o *option.Option) {

ginkgo.It("should display detailed information on multiple networks", func() {
command.Run(o, "network", "create", testNetwork)
lines := command.StdOutAsLines(o, "network", "inspect", bridgeNetwork, testNetwork, "--format", "{{.Name}}")
lines := command.StdoutAsLines(o, "network", "inspect", bridgeNetwork, testNetwork, "--format", "{{.Name}}")
gomega.Expect(lines).Should(gomega.ConsistOf(bridgeNetwork, testNetwork))
})
})
Expand Down
2 changes: 1 addition & 1 deletion tests/network_ls.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func NetworkLs(o *option.Option) {
})

ginkgo.It("should only display network name with --format flag", func() {
lines := command.StdOutAsLines(o, "network", "ls", "--format", "{{.Name}}")
lines := command.StdoutAsLines(o, "network", "ls", "--format", "{{.Name}}")
gomega.Expect(lines).Should(gomega.ContainElement(bridgeNetwork))
})

Expand Down
6 changes: 3 additions & 3 deletions tests/network_rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ func NetworkRm(o *option.Option) {
})

ginkgo.It("should remove a network", func() {
gomega.Expect(command.StdOutAsLines(o, "network", "ls", "--format", "{{.Name}}")).Should(gomega.ContainElement(testNetwork))
gomega.Expect(command.StdoutAsLines(o, "network", "ls", "--format", "{{.Name}}")).Should(gomega.ContainElement(testNetwork))
command.Run(o, "network", "rm", testNetwork)
gomega.Expect(command.StdOutAsLines(o, "network", "ls", "--format", "{{.Name}}")).ShouldNot(gomega.ContainElement(testNetwork))
gomega.Expect(command.StdoutAsLines(o, "network", "ls", "--format", "{{.Name}}")).ShouldNot(gomega.ContainElement(testNetwork))
})

ginkgo.It("should remove multiple networks", func() {
const testNetwork2 = "test-network2"
command.Run(o, "network", "create", testNetwork2)
command.Run(o, "network", "rm", testNetwork, testNetwork2)
lines := command.StdOutAsLines(o, "network", "ls", "--format", "{{.Name}}")
lines := command.StdoutAsLines(o, "network", "ls", "--format", "{{.Name}}")
gomega.Expect(lines).ShouldNot(gomega.ContainElement(testNetwork))
gomega.Expect(lines).ShouldNot(gomega.ContainElement(testNetwork2))
})
Expand Down
14 changes: 7 additions & 7 deletions tests/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Run(o *RunOption) {
})

ginkgo.It("should not echo dummy output if running with -d flag", func() {
output := command.StdOut(o.BaseOpt, "run", "-d", testImageName)
output := command.Stdout(o.BaseOpt, "run", "-d", testImageName)
gomega.Expect(output).ShouldNot(gomega.ContainSubstring("finch-test-dummy-output"))
})
})
Expand Down Expand Up @@ -108,7 +108,7 @@ func Run(o *RunOption) {
}()
command.Run(o.BaseOpt, "build", "-q", "-t", testImageName, buildContext)

envOutput := command.StdOut(o.BaseOpt, "run", "--rm", "--entrypoint", "time", testImageName, "echo", "blah")
envOutput := command.Stdout(o.BaseOpt, "run", "--rm", "--entrypoint", "time", testImageName, "echo", "blah")
gomega.Expect(envOutput).NotTo(gomega.ContainSubstring("foo"))
gomega.Expect(envOutput).NotTo(gomega.ContainSubstring("bar"))
gomega.Expect(envOutput).To(gomega.ContainSubstring("blah"))
Expand All @@ -123,7 +123,7 @@ func Run(o *RunOption) {

for _, env := range []string{"-e", "--env"} {
ginkgo.It(fmt.Sprintf("with %s flag, environment variables should be set in the container", env), func() {
envOutput := command.StdOut(o.BaseOpt, "run", "--rm",
envOutput := command.Stdout(o.BaseOpt, "run", "--rm",
"--env", "FOO=BAR", "--env", "FOO1", "-e", "ENV1=1", "-e", "ENV1=2",
defaultImage, "env")
gomega.Expect(envOutput).To(gomega.ContainSubstring("FOO=BAR"))
Expand All @@ -137,7 +137,7 @@ func Run(o *RunOption) {
envPath := ffs.CreateTempFile("env", envPair)
ginkgo.DeferCleanup(os.RemoveAll, filepath.Dir(envPath))

envOutput := command.StdOut(o.BaseOpt, "run", "--rm", "--env-file", envPath, defaultImage, "env")
envOutput := command.Stdout(o.BaseOpt, "run", "--rm", "--env-file", envPath, defaultImage, "env")
gomega.Expect(envOutput).To(gomega.ContainSubstring(envPair))
})
})
Expand Down Expand Up @@ -261,21 +261,21 @@ func Run(o *RunOption) {

ginkgo.It("should be able to set custom DNS servers with --dns flag", func() {
const nameserver = "10.10.10.10"
lines := command.StdOutAsLines(o.BaseOpt, "run", "--dns", nameserver, "--name", testContainerName,
lines := command.StdoutAsLines(o.BaseOpt, "run", "--dns", nameserver, "--name", testContainerName,
defaultImage, "cat", "/etc/resolv.conf")
gomega.Expect(lines).Should(gomega.ContainElement(fmt.Sprintf("nameserver %s", nameserver)))
})

ginkgo.It("should be able to set custom DNS search domains with --dns-search flag", func() {
lines := command.StdOutAsLines(o.BaseOpt, "run", "--dns-search", "test", "--name", testContainerName,
lines := command.StdoutAsLines(o.BaseOpt, "run", "--dns-search", "test", "--name", testContainerName,
defaultImage, "cat", "/etc/resolv.conf")
gomega.Expect(lines).Should(gomega.ContainElement("search test"))
})

for _, dnsOption := range []string{"--dns-opt", "--dns-option"} {
dnsOption := dnsOption
ginkgo.It(fmt.Sprintf("should be able to set DNS option with %s flag", dnsOption), func() {
lines := command.StdOutAsLines(o.BaseOpt, "run", dnsOption, "debug", "--name", testContainerName,
lines := command.StdoutAsLines(o.BaseOpt, "run", dnsOption, "debug", "--name", testContainerName,
defaultImage, "cat", "/etc/resolv.conf")
gomega.Expect(lines).Should(gomega.ContainElement("options debug"))
})
Expand Down
Loading