Skip to content

Commit

Permalink
test: add tests for fs functions (#29)
Browse files Browse the repository at this point in the history
Signed-off-by: Ruslan Semagin <[email protected]>
  • Loading branch information
pixel365 authored Feb 20, 2025
1 parent d443b5f commit 1b2e155
Show file tree
Hide file tree
Showing 2 changed files with 151 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ func copyFile(
}

func shouldSkip(path string, patterns *[]string) bool {
if patterns == nil || len(*patterns) == 0 {
return false
}

for _, pattern := range *patterns {
if ok, err := doublestar.PathMatch(pattern, path); ok || err != nil {
return true
Expand Down
147 changes: 147 additions & 0 deletions internal/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package internal

import (
"context"
"fmt"
"os"
"path/filepath"
"sync"
"testing"
"time"
)

func Test_mkdir(t *testing.T) {
t.Run("success", func(t *testing.T) {
name := fmt.Sprintf("./_%d", time.Now().UTC().Unix())
path, err := mkdir(name)
if err != nil {
t.Error(err)
}

defer func() {
if err := os.Remove(path); err != nil {
t.Error(err)
}
}()
})
}

func Test_zipIt(t *testing.T) {
t.Run("success", func(t *testing.T) {
name := fmt.Sprintf("./_%d", time.Now().UTC().Unix())
path, err := mkdir(name)
if err != nil {
t.Error(err)
}

defer func() {
if err := os.Remove(path); err != nil {
t.Error(err)
}
}()

archivePath := fmt.Sprintf("./_%d.zip", time.Now().UTC().Unix())
if err := zipIt(path, archivePath); err != nil {
t.Error(err)
}
defer func() {
if err := os.Remove(archivePath); err != nil {
t.Error(err)
}
}()
})
}

func Test_shouldSkip(t *testing.T) {
patterns := []string{
"**/*.log",
"*.json",
"**/*some*/*",
}
type args struct {
patterns *[]string
path string
}
tests := []struct {
args args
name string
want bool
}{
{args{nil, "."}, "1", false},
{args{&patterns, "./testing/errors.log"}, "2", true},
{args{&patterns, "./testing/errors.json"}, "3", false},
{args{&patterns, "errors.json"}, "4", true},
{args{&patterns, "./testing/data/awesome/cfg.yaml"}, "5", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := shouldSkip(tt.args.path, tt.args.patterns); got != tt.want {
t.Errorf("shouldSkip() = %v, want %v", got, tt.want)
}
})
}
}

func Test_copyFromPath(t *testing.T) {
t.Run("success", func(t *testing.T) {
from := fmt.Sprintf("./_%d", time.Now().UTC().Unix())
fromPath, err := mkdir(from)
if err != nil {
t.Error(err)
}

defer func() {
if err := os.Remove(fromPath); err != nil {
t.Error(err)
}
}()

to := fmt.Sprintf("./__%d", time.Now().UTC().Unix())
toPath, err := mkdir(to)
if err != nil {
t.Error(err)
}

defer func() {
if err := os.Remove(toPath); err != nil {
t.Error(err)
}
}()

fileName := fmt.Sprintf("%d.txt", time.Now().UTC().Unix())
filePath := filepath.Join(from, fileName)
filePath = filepath.Clean(filePath)
file, err := os.Create(filePath)
if err != nil {
t.Error(err)
}

err = file.Close()
if err != nil {
t.Error(err)
}

defer func() {
if err := os.Remove(filePath); err != nil {
t.Error(err)
}
}()

var wg sync.WaitGroup
errChan := make(chan error)
patterns := []string{
"**/*.log",
"*.json",
"**/*some*/*",
}

wg.Add(1)
copyFromPath(context.Background(), &wg, errChan, &patterns, from, to, Replace)

defer func() {
if err := os.Remove(fmt.Sprintf("%s/%s", toPath, fileName)); err != nil {
t.Error(err)
}
}()
})
}

0 comments on commit 1b2e155

Please sign in to comment.