Skip to content

Commit

Permalink
test: Added test to cover snippet directory bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RamiAwar committed Oct 24, 2024
1 parent 92c73b7 commit 13e3074
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cmd/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,10 @@ func countSnippetLines() int {
// new creates a new snippet and saves it to the main snippet file
// then syncs the snippet file if configured to do so.
func new(cmd *cobra.Command, args []string) (err error) {
return _new(os.Stdin, os.Stdout, args)
}

func _new(in io.ReadCloser, out io.Writer, args []string) (err error) {
var filename string = ""
var command string
var description string
Expand All @@ -203,7 +207,7 @@ func new(cmd *cobra.Command, args []string) (err error) {
command, err = scanMultiLine(
color.YellowString("Command> "),
color.YellowString(".......> "),
os.Stdout, os.Stdin,
out, in,
)
} else if config.Flag.UseEditor {
// Create and save empty snippet
Expand All @@ -216,20 +220,20 @@ func new(cmd *cobra.Command, args []string) (err error) {
return createAndEditSnippet(newSnippet, snippets, lineCount+3)

} else {
command, err = scan(color.HiYellowString("Command> "), os.Stdout, os.Stdin, false)
command, err = scan(color.HiYellowString("Command> "), out, in, false)
}
if err != nil {
return err
}
}
description, err = scan(color.HiGreenString("Description> "), os.Stdout, os.Stdin, false)
description, err = scan(color.HiGreenString("Description> "), out, in, false)
if err != nil {
return err
}

if config.Flag.Tag {
var t string
if t, err = scan(color.HiCyanString("Tag> "), os.Stdout, os.Stdin, true); err != nil {
if t, err = scan(color.HiCyanString("Tag> "), out, in, true); err != nil {
return err
}

Expand Down
124 changes: 124 additions & 0 deletions cmd/new_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@ package cmd

import (
"bytes"
"os"
"path/filepath"
"strings"
"testing"

"github.com/knqyf263/pet/config"
"github.com/knqyf263/pet/snippet"
"github.com/pelletier/go-toml"
)

// MockReadCloser is a mock implementation of io.ReadCloser
Expand Down Expand Up @@ -127,3 +133,121 @@ func TestScanMultiLine_ExitsOnTwoEmptyLines(t *testing.T) {
t.Errorf("Expected error %v, but got %v", expectedError, err)
}
}

func TestNewSnippetCreationWithSnippetDirectory(t *testing.T) {
// Setup temporary directory for config
tempDir := t.TempDir()
tempSnippetFile := filepath.Join(tempDir, "snippet.toml")
tempSnippetDir1 := filepath.Join(tempDir, "snippets1")
tempSnippetDir2 := filepath.Join(tempDir, "snippets2")

// Create snippet directories
if err := os.Mkdir(tempSnippetDir1, 0755); err != nil {
t.Fatalf("Failed to create temp snippet directory: %v", err)
}
if err := os.Mkdir(tempSnippetDir2, 0755); err != nil {
t.Fatalf("Failed to create temp snippet directory: %v", err)
}

// Create dummy snippets in the main snippet file
mainSnippets := snippet.Snippets{
Snippets: []snippet.SnippetInfo{
{Description: "main snippet 1", Command: "echo main1"},
{Description: "main snippet 2", Command: "echo main2"},
},
}
saveSnippetsToFile(t, tempSnippetFile, mainSnippets)

// Create dummy snippets in the snippet directories
dirSnippets1 := snippet.Snippets{
Snippets: []snippet.SnippetInfo{
{Description: "dir1 snippet 1", Command: "echo dir1-1"},
},
}
dirSnippets2 := snippet.Snippets{
Snippets: []snippet.SnippetInfo{
{Description: "dir2 snippet 1", Command: "echo dir2-1"},
},
}
saveSnippetsToFile(t, filepath.Join(tempSnippetDir1, "snippets1.toml"), dirSnippets1)
saveSnippetsToFile(t, filepath.Join(tempSnippetDir2, "snippets2.toml"), dirSnippets2)

// Mock configuration
config.Conf.General.SnippetFile = tempSnippetFile
config.Conf.General.SnippetDirs = []string{tempSnippetDir1, tempSnippetDir2}

// Simulate creating a new snippet
args := []string{"echo new command"}

// Create a buffer for output
var outputBuffer bytes.Buffer
// Create a mock ReadCloser for input
inputReader := &MockReadCloser{strings.NewReader("test\ntest")}

err := _new(inputReader, &outputBuffer, args)
if err != nil {
t.Fatalf("Failed to create new snippet: %v", err)
}

// Load the main snippet file and check:
// 1 - if the new snippet is added
// 2 - if the number of snippets is correct (to avoid bugs like overwriting with dir snippets)
var updatedMainSnippets snippet.Snippets
loadSnippetsFromFile(t, tempSnippetFile, &updatedMainSnippets)

if len(updatedMainSnippets.Snippets) != 3 {
t.Fatalf("Expected 3 snippets in main snippet file, got %d", len(updatedMainSnippets.Snippets))
}

newSnippet := updatedMainSnippets.Snippets[2]
if newSnippet.Command != "echo new command" {
t.Errorf("Expected new command to be 'echo new command', got '%s'", newSnippet.Command)
}

// Ensure the snippet files in the directories remain unchanged
var unchangedDirSnippets1, unchangedDirSnippets2 snippet.Snippets
loadSnippetsFromFile(t, filepath.Join(tempSnippetDir1, "snippets1.toml"), &unchangedDirSnippets1)
loadSnippetsFromFile(t, filepath.Join(tempSnippetDir2, "snippets2.toml"), &unchangedDirSnippets2)

if !compareSnippets(dirSnippets1, unchangedDirSnippets1) {
t.Errorf("Snippets in directory 1 have changed")
}
if !compareSnippets(dirSnippets2, unchangedDirSnippets2) {
t.Errorf("Snippets in directory 2 have changed")
}
}

func saveSnippetsToFile(t *testing.T, filename string, snippets snippet.Snippets) {
f, err := os.Create(filename)
if err != nil {
t.Fatalf("Failed to create snippet file: %v", err)
}
defer f.Close()

if err := toml.NewEncoder(f).Encode(snippets); err != nil {
t.Fatalf("Failed to encode snippets to file: %v", err)
}
}

func loadSnippetsFromFile(t *testing.T, filename string, snippets *snippet.Snippets) {
f, err := os.ReadFile(filename)
if err != nil {
t.Fatalf("Failed to read snippet file: %v", err)
}

if err := toml.Unmarshal(f, snippets); err != nil {
t.Fatalf("Failed to unmarshal snippets from file: %v", err)
}
}

func compareSnippets(a, b snippet.Snippets) bool {
if len(a.Snippets) != len(b.Snippets) {
return false
}
for i := range a.Snippets {
if a.Snippets[i].Description != b.Snippets[i].Description || a.Snippets[i].Command != b.Snippets[i].Command {
return false
}
}
return true
}

0 comments on commit 13e3074

Please sign in to comment.