Skip to content

Commit

Permalink
Improve cmd test coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Prodan <stefan.prodan@gmail.com>
  • Loading branch information
stefanprodan committed Nov 30, 2023
1 parent 3798930 commit 804b353
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
8 changes: 8 additions & 0 deletions cmd/timoni/artifact_push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ func Test_PushArtifact(t *testing.T) {
g.Expect(err).ToNot(HaveOccurred())
g.Expect(output).To(ContainSubstring(aURL))

// List the artifacts
output, err = executeCommand(fmt.Sprintf(
"artifact list oci://%s",
aURL,
))
g.Expect(err).ToNot(HaveOccurred())
g.Expect(output).To(ContainSubstring(aTag))

// Pull the artifact from registry
image, err := crane.Pull(fmt.Sprintf("%s:%s", aURL, aTag))
g.Expect(err).ToNot(HaveOccurred())
Expand Down
48 changes: 48 additions & 0 deletions cmd/timoni/dyff_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
Copyright 2023 Stefan Prodan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"bytes"
"os"
"testing"

. "github.com/onsi/gomega"
)

func TestDiffYAML(t *testing.T) {
g := NewWithT(t)

liveFile, err := os.CreateTemp("", "live")
g.Expect(err).ToNot(HaveOccurred())
defer os.Remove(liveFile.Name())

mergedFile, err := os.CreateTemp("", "merged")
g.Expect(err).ToNot(HaveOccurred())
defer os.Remove(mergedFile.Name())

err = os.WriteFile(liveFile.Name(), []byte("apiVersion: v1\nkind: Pod\nmetadata:\n name: test-pod\n"), 0644)
g.Expect(err).ToNot(HaveOccurred())

err = os.WriteFile(mergedFile.Name(), []byte("apiVersion: v1\nkind: Pod\nmetadata:\n name: test-pod-merged\n"), 0644)
g.Expect(err).ToNot(HaveOccurred())

buf := new(bytes.Buffer)
err = diffYAML(liveFile.Name(), mergedFile.Name(), buf)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(buf.String()).To(ContainSubstring("name: test-pod-merged"))
}
5 changes: 4 additions & 1 deletion cmd/timoni/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ func runVersionCmd(cmd *cobra.Command, args []string) error {
return err
}

cmd.OutOrStdout().Write(marshalled)
_, err = cmd.OutOrStdout().Write(marshalled)
if err != nil {
return err
}

return nil
}
41 changes: 41 additions & 0 deletions cmd/timoni/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
Copyright 2023 Stefan Prodan
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
"testing"

. "github.com/onsi/gomega"
"sigs.k8s.io/yaml"

apiv1 "github.com/stefanprodan/timoni/api/v1alpha1"
)

func TestVersion(t *testing.T) {
g := NewWithT(t)
output, err := executeCommand("version -o yaml")
g.Expect(err).ToNot(HaveOccurred())

var data map[string]interface{}
err = yaml.Unmarshal([]byte(output), &data)
g.Expect(err).ToNot(HaveOccurred())

expectedAPIVersion := apiv1.GroupVersion.String()
g.Expect(data).To(HaveKeyWithValue("api", expectedAPIVersion))
g.Expect(data).To(HaveKey("client"))
g.Expect(data).To(HaveKey("cue"))
}

0 comments on commit 804b353

Please sign in to comment.