Skip to content

Commit

Permalink
feat: Add Unit tests (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pradumnasaraf authored Jul 12, 2023
1 parent 2f04f1f commit 425593c
Show file tree
Hide file tree
Showing 21 changed files with 376 additions and 19 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Unit Tests

on:
push:
branches: ["main"]
pull_request:
branches: ["main"]

jobs:
build-format:
name: Build and Format
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v3

- name: Setup Go
uses: actions/setup-go@v4
with:
go-version: 1.19

- name: Install Executable to $GOPATH/bin
run: go install

- name: Run Tests
run: go test -v ./tests/...
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,6 @@ candy

.env
dist/
output.json
output.yaml
output.yml
16 changes: 8 additions & 8 deletions cmd/jsonToYaml.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand All @@ -12,8 +12,8 @@ var (
outputYamlFile string
)

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

if outputYamlFile == "" {
log.Print("Operation completed successfully. Check the output.yaml file.")
fmt.Println("Operation completed successfully. Check the output.yaml file.")
} else {
log.Print("Operation completed successfully. Check the " + outputYamlFile + " file.")
fmt.Println("Operation completed successfully. Check the " + outputYamlFile + " file.")
}
},
}

func init() {

// Flags for the JYT command
jsonToYaml.Flags().StringVarP(&outputYamlFile, "output", "o", "", "Output YAML file name (default is output.yaml)")
jsonToYaml.Flags().StringVarP(&inputJsonFile, "file", "f", "", "Input the JSON file name")
err := jsonToYaml.MarkFlagRequired("file")
jsonToYamlCmd.Flags().StringVarP(&outputYamlFile, "output", "o", "", "Output YAML file name (default is output.yaml)")
jsonToYamlCmd.Flags().StringVarP(&inputJsonFile, "file", "f", "", "Input the JSON file name")
err := jsonToYamlCmd.MarkFlagRequired("file")
checkNilErr(err)
}
13 changes: 8 additions & 5 deletions cmd/keyValueToJson.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
Expand All @@ -15,7 +16,7 @@ var (
)

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

_, err = file.WriteString(string(jsonString))
checkNilErr(err)

fmt.Println("Operation completed successfully. Check the", outputJsonFile1, "file.")
},
}

func init() {

// Flags for the TTJ command
keyValueToJson.Flags().StringVarP(&inputTextFile, "file", "f", "", "Input the text file name. Eg: keys.txt or .env")
err := keyValueToJson.MarkFlagRequired("file")
keyValueToJsonCmd.Flags().StringVarP(&inputTextFile, "file", "f", "", "Input the text file name. Eg: keys.txt or .env")
err := keyValueToJsonCmd.MarkFlagRequired("file")
checkNilErr(err)

keyValueToJson.Flags().StringVarP(&outputJsonFile1, "output", "o", "", "Output JSON file name (default is output.json)")
keyValueToJson.Flags().BoolP("print", "p", false, "Print the output to the console")
keyValueToJsonCmd.Flags().StringVarP(&outputJsonFile1, "output", "o", "", "Output JSON file name (default is output.json)")
keyValueToJsonCmd.Flags().BoolP("print", "p", false, "Print the output to the console")
}
4 changes: 2 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func init() {

// Subcommands for the root command
rootCmd.AddCommand(encodeCmd)
rootCmd.AddCommand(jsonToYaml)
rootCmd.AddCommand(versionCmd)
rootCmd.AddCommand(yamlToJsonCmd)
rootCmd.AddCommand(keyValueToJson)
rootCmd.AddCommand(jsonToYamlCmd)
rootCmd.AddCommand(docker.DockerCmd)
rootCmd.AddCommand(keyValueToJsonCmd)
rootCmd.AddCommand(kubernetes.KubernetesCmd)

}
Expand Down
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ var (
)

const (
CLI_VERSION = "1.5.0"
CLI_VERSION = "1.6.0"
OWNER = "Pradumnasaraf"
REPO = "candy"
)
Expand Down
6 changes: 3 additions & 3 deletions cmd/yamlToJson.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cmd

import (
"log"
"fmt"

"github.com/spf13/cobra"
"github.com/spf13/viper"
Expand Down Expand Up @@ -33,9 +33,9 @@ var yamlToJsonCmd = &cobra.Command{
checkNilErr(err)

if outputJsonFile == "" {
log.Print("Operation completed successfully. Check the output.json file.")
fmt.Println("Operation completed successfully. Check the output.json file.")
} else {
log.Print("Operation completed successfully. Check the " + outputJsonFile + " file.")
fmt.Println("Operation completed successfully. Check the " + outputJsonFile + " file.")
}
},
}
Expand Down
28 changes: 28 additions & 0 deletions tests/docker/docker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package docker

import (
"os/exec"
"strings"
"testing"
)

func TestDockerCmd(t *testing.T) {

expectedOutput := "Docker related commands."

// it should convert a key-value file to json
cmd := exec.Command("candy", "docker")

// Capture the output
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

// Validate the cli output
got := strings.TrimSpace(string(output)[:24])
if got != expectedOutput {
t.Errorf("expected %v, but got: %v", expectedOutput, got)
}

}
55 changes: 55 additions & 0 deletions tests/encode_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package test

import (
"encoding/base64"
"os/exec"
"strings"
"testing"
)

// Test encode command
func TestEncodeCmd(t *testing.T) {
stringToEncode := "password"

// Execute the encode command
cmd := exec.Command("candy", "encode", stringToEncode)

// Capture the output
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

// Validate the output
expectedOutput := base64.StdEncoding.EncodeToString([]byte(stringToEncode))
got := strings.TrimSpace(string(output)) // Convert output to string and remove leading/trailing spaces
if got != expectedOutput {
t.Errorf("expected %v, but got: %v", expectedOutput, got)
}
}

// Test decode flag
func TestEncodeCmdWithDecode(t *testing.T) {
stringToDecode := "cGFzc3dvcmQ="

// Execute the encode command
cmd := exec.Command("candy", "encode", "-d", stringToDecode)

// Capture the output
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

// Validate the output
expectedOutput, err := base64.StdEncoding.DecodeString(stringToDecode)
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

got := strings.TrimSpace(string(output))
if got != string(expectedOutput) {
t.Errorf("expected %v, but got: %v", string(expectedOutput), got)
}

}
39 changes: 39 additions & 0 deletions tests/jsonToYaml_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package test

import (
"os/exec"
"strings"
"testing"
)

func TestJsonToYamlCmd(t *testing.T) {

// it should convert a json file to yaml

// Execute the jsonToYaml command
cmd := exec.Command("candy", "JTY", "-f", "testdata/JTY.json")

// Capture the output
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

// Validate the cli output
expecredOutput := "Operation completed successfully. Check the output.yaml file."
got := strings.TrimSpace(string(output))
if got != expecredOutput {
t.Errorf("expected %v, but got: %v", expecredOutput, got)
}

// Validate the output file with a new
cmd1 := exec.Command("diff", "testdata/JTY_output.yaml", "output.yaml")
output, err = cmd1.CombinedOutput()
if err != nil {
t.Errorf("Error comparing output.yaml and testdata/test.yaml")
}
if string(output) != "" {
t.Errorf("Expected output.yaml and testdata/test.yaml to be the same, but got error")
}

}
37 changes: 37 additions & 0 deletions tests/keyValueToJson_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package test

import (
"os/exec"
"strings"
"testing"
)

func TestKeyValueToJson(t *testing.T) {

// it should convert a key-value file to json
cmd := exec.Command("candy", "KVTJ", "-f", "testdata/env")

// Capture the output
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

// Validate the cli output
expecredOutput := "Operation completed successfully. Check the output.json file."
got := strings.TrimSpace(string(output))
if got != expecredOutput {
t.Errorf("expected %v, but got: %v", expecredOutput, got)
}

// Validate the output file with a new
cmd1 := exec.Command("diff", "testdata/env_output.json", "output.json")
output, err = cmd1.CombinedOutput()
if err != nil {
t.Errorf("Error comparing output.json and testdata/test.json")
}
if string(output) != "" {
t.Errorf("Expected output.json and testdata/test.json to be the same, but got error")
}

}
27 changes: 27 additions & 0 deletions tests/kubernetes/kubernetes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package docker

import (
"os/exec"
"strings"
"testing"
)

func TestKubernetesCmd(t *testing.T) {

expectedOutput := "Kubernetes related commands."

cmd := exec.Command("candy", "k8s")

// Capture the output
output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

// Validate the cli output
got := strings.TrimSpace(string(output)[:28])
if got != expectedOutput {
t.Errorf("expected %v, but got: %v", expectedOutput, got)
}

}
25 changes: 25 additions & 0 deletions tests/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package test

import (
"os/exec"
"strings"
"testing"
)

func TestRootCmd(t *testing.T) {

expectedOutput := "Do all your tedious tasks with a single command"

cmd := exec.Command("candy")

output, err := cmd.CombinedOutput()
if err != nil {
t.Errorf("expected no error, but got: %v", err)
}

got := strings.TrimSpace(string(output)[:47])
if got != expectedOutput {
t.Errorf("expected %v, but got: %v", expectedOutput, got)
}

}
12 changes: 12 additions & 0 deletions tests/testdata/JTY.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"name": "JSON TO KEY TEST",
"version": "1.0.0",
"tag": [
"json",
"key"
],
"properties": {
"name": "test",
"type": "string"
}
}
Loading

0 comments on commit 425593c

Please sign in to comment.