Skip to content

Commit 425593c

Browse files
feat: Add Unit tests (#23)
1 parent 2f04f1f commit 425593c

21 files changed

+376
-19
lines changed

.github/workflows/unit-test.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Unit Tests
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
8+
9+
jobs:
10+
build-format:
11+
name: Build and Format
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Code
15+
uses: actions/checkout@v3
16+
17+
- name: Setup Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: 1.19
21+
22+
- name: Install Executable to $GOPATH/bin
23+
run: go install
24+
25+
- name: Run Tests
26+
run: go test -v ./tests/...

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,6 @@ candy
4747

4848
.env
4949
dist/
50+
output.json
51+
output.yaml
52+
output.yml

cmd/jsonToYaml.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"log"
4+
"fmt"
55

66
"github.com/spf13/cobra"
77
"github.com/spf13/viper"
@@ -12,8 +12,8 @@ var (
1212
outputYamlFile string
1313
)
1414

15-
// jsonToYaml is the command for converting JSON to YAML
16-
var jsonToYaml = &cobra.Command{
15+
// jsonToYamlCmd is the command for converting JSON to YAML
16+
var jsonToYamlCmd = &cobra.Command{
1717
Use: "JTY [flags]",
1818
Short: "Converts a JSON into YAML.",
1919
Run: func(cmd *cobra.Command, args []string) {
@@ -34,18 +34,18 @@ var jsonToYaml = &cobra.Command{
3434
checkNilErr(err)
3535

3636
if outputYamlFile == "" {
37-
log.Print("Operation completed successfully. Check the output.yaml file.")
37+
fmt.Println("Operation completed successfully. Check the output.yaml file.")
3838
} else {
39-
log.Print("Operation completed successfully. Check the " + outputYamlFile + " file.")
39+
fmt.Println("Operation completed successfully. Check the " + outputYamlFile + " file.")
4040
}
4141
},
4242
}
4343

4444
func init() {
4545

4646
// Flags for the JYT command
47-
jsonToYaml.Flags().StringVarP(&outputYamlFile, "output", "o", "", "Output YAML file name (default is output.yaml)")
48-
jsonToYaml.Flags().StringVarP(&inputJsonFile, "file", "f", "", "Input the JSON file name")
49-
err := jsonToYaml.MarkFlagRequired("file")
47+
jsonToYamlCmd.Flags().StringVarP(&outputYamlFile, "output", "o", "", "Output YAML file name (default is output.yaml)")
48+
jsonToYamlCmd.Flags().StringVarP(&inputJsonFile, "file", "f", "", "Input the JSON file name")
49+
err := jsonToYamlCmd.MarkFlagRequired("file")
5050
checkNilErr(err)
5151
}

cmd/keyValueToJson.go

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package cmd
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"log"
67
"os"
78
"strings"
@@ -15,7 +16,7 @@ var (
1516
)
1617

1718
// textToJsonCmd represents the aa command
18-
var keyValueToJson = &cobra.Command{
19+
var keyValueToJsonCmd = &cobra.Command{
1920
Use: "KVTJ [flags]",
2021
Short: "Converts Key-Value (text) to JSON.",
2122
Run: func(cmd *cobra.Command, args []string) {
@@ -88,16 +89,18 @@ var keyValueToJson = &cobra.Command{
8889

8990
_, err = file.WriteString(string(jsonString))
9091
checkNilErr(err)
92+
93+
fmt.Println("Operation completed successfully. Check the", outputJsonFile1, "file.")
9194
},
9295
}
9396

9497
func init() {
9598

9699
// Flags for the TTJ command
97-
keyValueToJson.Flags().StringVarP(&inputTextFile, "file", "f", "", "Input the text file name. Eg: keys.txt or .env")
98-
err := keyValueToJson.MarkFlagRequired("file")
100+
keyValueToJsonCmd.Flags().StringVarP(&inputTextFile, "file", "f", "", "Input the text file name. Eg: keys.txt or .env")
101+
err := keyValueToJsonCmd.MarkFlagRequired("file")
99102
checkNilErr(err)
100103

101-
keyValueToJson.Flags().StringVarP(&outputJsonFile1, "output", "o", "", "Output JSON file name (default is output.json)")
102-
keyValueToJson.Flags().BoolP("print", "p", false, "Print the output to the console")
104+
keyValueToJsonCmd.Flags().StringVarP(&outputJsonFile1, "output", "o", "", "Output JSON file name (default is output.json)")
105+
keyValueToJsonCmd.Flags().BoolP("print", "p", false, "Print the output to the console")
103106
}

cmd/root.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,11 @@ func init() {
3131

3232
// Subcommands for the root command
3333
rootCmd.AddCommand(encodeCmd)
34-
rootCmd.AddCommand(jsonToYaml)
3534
rootCmd.AddCommand(versionCmd)
3635
rootCmd.AddCommand(yamlToJsonCmd)
37-
rootCmd.AddCommand(keyValueToJson)
36+
rootCmd.AddCommand(jsonToYamlCmd)
3837
rootCmd.AddCommand(docker.DockerCmd)
38+
rootCmd.AddCommand(keyValueToJsonCmd)
3939
rootCmd.AddCommand(kubernetes.KubernetesCmd)
4040

4141
}

cmd/version.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ var (
1212
)
1313

1414
const (
15-
CLI_VERSION = "1.5.0"
15+
CLI_VERSION = "1.6.0"
1616
OWNER = "Pradumnasaraf"
1717
REPO = "candy"
1818
)

cmd/yamlToJson.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package cmd
22

33
import (
4-
"log"
4+
"fmt"
55

66
"github.com/spf13/cobra"
77
"github.com/spf13/viper"
@@ -33,9 +33,9 @@ var yamlToJsonCmd = &cobra.Command{
3333
checkNilErr(err)
3434

3535
if outputJsonFile == "" {
36-
log.Print("Operation completed successfully. Check the output.json file.")
36+
fmt.Println("Operation completed successfully. Check the output.json file.")
3737
} else {
38-
log.Print("Operation completed successfully. Check the " + outputJsonFile + " file.")
38+
fmt.Println("Operation completed successfully. Check the " + outputJsonFile + " file.")
3939
}
4040
},
4141
}

tests/docker/docker_test.go

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package docker
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestDockerCmd(t *testing.T) {
10+
11+
expectedOutput := "Docker related commands."
12+
13+
// it should convert a key-value file to json
14+
cmd := exec.Command("candy", "docker")
15+
16+
// Capture the output
17+
output, err := cmd.CombinedOutput()
18+
if err != nil {
19+
t.Errorf("expected no error, but got: %v", err)
20+
}
21+
22+
// Validate the cli output
23+
got := strings.TrimSpace(string(output)[:24])
24+
if got != expectedOutput {
25+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
26+
}
27+
28+
}

tests/encode_test.go

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package test
2+
3+
import (
4+
"encoding/base64"
5+
"os/exec"
6+
"strings"
7+
"testing"
8+
)
9+
10+
// Test encode command
11+
func TestEncodeCmd(t *testing.T) {
12+
stringToEncode := "password"
13+
14+
// Execute the encode command
15+
cmd := exec.Command("candy", "encode", stringToEncode)
16+
17+
// Capture the output
18+
output, err := cmd.CombinedOutput()
19+
if err != nil {
20+
t.Errorf("expected no error, but got: %v", err)
21+
}
22+
23+
// Validate the output
24+
expectedOutput := base64.StdEncoding.EncodeToString([]byte(stringToEncode))
25+
got := strings.TrimSpace(string(output)) // Convert output to string and remove leading/trailing spaces
26+
if got != expectedOutput {
27+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
28+
}
29+
}
30+
31+
// Test decode flag
32+
func TestEncodeCmdWithDecode(t *testing.T) {
33+
stringToDecode := "cGFzc3dvcmQ="
34+
35+
// Execute the encode command
36+
cmd := exec.Command("candy", "encode", "-d", stringToDecode)
37+
38+
// Capture the output
39+
output, err := cmd.CombinedOutput()
40+
if err != nil {
41+
t.Errorf("expected no error, but got: %v", err)
42+
}
43+
44+
// Validate the output
45+
expectedOutput, err := base64.StdEncoding.DecodeString(stringToDecode)
46+
if err != nil {
47+
t.Errorf("expected no error, but got: %v", err)
48+
}
49+
50+
got := strings.TrimSpace(string(output))
51+
if got != string(expectedOutput) {
52+
t.Errorf("expected %v, but got: %v", string(expectedOutput), got)
53+
}
54+
55+
}

tests/jsonToYaml_test.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package test
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestJsonToYamlCmd(t *testing.T) {
10+
11+
// it should convert a json file to yaml
12+
13+
// Execute the jsonToYaml command
14+
cmd := exec.Command("candy", "JTY", "-f", "testdata/JTY.json")
15+
16+
// Capture the output
17+
output, err := cmd.CombinedOutput()
18+
if err != nil {
19+
t.Errorf("expected no error, but got: %v", err)
20+
}
21+
22+
// Validate the cli output
23+
expecredOutput := "Operation completed successfully. Check the output.yaml file."
24+
got := strings.TrimSpace(string(output))
25+
if got != expecredOutput {
26+
t.Errorf("expected %v, but got: %v", expecredOutput, got)
27+
}
28+
29+
// Validate the output file with a new
30+
cmd1 := exec.Command("diff", "testdata/JTY_output.yaml", "output.yaml")
31+
output, err = cmd1.CombinedOutput()
32+
if err != nil {
33+
t.Errorf("Error comparing output.yaml and testdata/test.yaml")
34+
}
35+
if string(output) != "" {
36+
t.Errorf("Expected output.yaml and testdata/test.yaml to be the same, but got error")
37+
}
38+
39+
}

tests/keyValueToJson_test.go

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package test
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestKeyValueToJson(t *testing.T) {
10+
11+
// it should convert a key-value file to json
12+
cmd := exec.Command("candy", "KVTJ", "-f", "testdata/env")
13+
14+
// Capture the output
15+
output, err := cmd.CombinedOutput()
16+
if err != nil {
17+
t.Errorf("expected no error, but got: %v", err)
18+
}
19+
20+
// Validate the cli output
21+
expecredOutput := "Operation completed successfully. Check the output.json file."
22+
got := strings.TrimSpace(string(output))
23+
if got != expecredOutput {
24+
t.Errorf("expected %v, but got: %v", expecredOutput, got)
25+
}
26+
27+
// Validate the output file with a new
28+
cmd1 := exec.Command("diff", "testdata/env_output.json", "output.json")
29+
output, err = cmd1.CombinedOutput()
30+
if err != nil {
31+
t.Errorf("Error comparing output.json and testdata/test.json")
32+
}
33+
if string(output) != "" {
34+
t.Errorf("Expected output.json and testdata/test.json to be the same, but got error")
35+
}
36+
37+
}

tests/kubernetes/kubernetes_test.go

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package docker
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestKubernetesCmd(t *testing.T) {
10+
11+
expectedOutput := "Kubernetes related commands."
12+
13+
cmd := exec.Command("candy", "k8s")
14+
15+
// Capture the output
16+
output, err := cmd.CombinedOutput()
17+
if err != nil {
18+
t.Errorf("expected no error, but got: %v", err)
19+
}
20+
21+
// Validate the cli output
22+
got := strings.TrimSpace(string(output)[:28])
23+
if got != expectedOutput {
24+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
25+
}
26+
27+
}

tests/root_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package test
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
func TestRootCmd(t *testing.T) {
10+
11+
expectedOutput := "Do all your tedious tasks with a single command"
12+
13+
cmd := exec.Command("candy")
14+
15+
output, err := cmd.CombinedOutput()
16+
if err != nil {
17+
t.Errorf("expected no error, but got: %v", err)
18+
}
19+
20+
got := strings.TrimSpace(string(output)[:47])
21+
if got != expectedOutput {
22+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
23+
}
24+
25+
}

tests/testdata/JTY.json

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"name": "JSON TO KEY TEST",
3+
"version": "1.0.0",
4+
"tag": [
5+
"json",
6+
"key"
7+
],
8+
"properties": {
9+
"name": "test",
10+
"type": "string"
11+
}
12+
}

0 commit comments

Comments
 (0)