Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for 'klone sync' and editKloneFile function #4

Merged
merged 5 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions .github/workflows/build.yml → .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: build
name: tests

on:
pull_request:
Expand All @@ -8,7 +8,7 @@ permissions:
contents: read

jobs:
build:
unit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -17,3 +17,13 @@ jobs:
go-version: 1.21
cache: true
- run: go test -v ./...

e2e:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version: 1.21
cache: true
- run: ./test-e2e.sh
4 changes: 2 additions & 2 deletions example/klone.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
targets:
projects:
- folder_name: kube_apis
repo_url: git@github.com:kubernetes/kubernetes.git
repo_url: https://github.com/kubernetes/kubernetes.git
repo_ref: master
repo_hash: 56d7898510f2a973f92fda13c2ba3a5e756d9621
repo_path: api
- folder_name: kube_changelog
repo_url: git@github.com:kubernetes/kubernetes.git
repo_url: https://github.com/kubernetes/kubernetes.git
repo_ref: master
repo_hash: 56d7898510f2a973f92fda13c2ba3a5e756d9621
repo_path: CHANGELOG
3 changes: 3 additions & 0 deletions pkg/mod/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ func (w WorkDir) editKloneFile(fn func(*kloneFile) error) error {
reader := bufio.NewReader(file)
for {
line, isPrefix, err := reader.ReadLine()
if err == io.EOF {
break
}
if err != nil {
return err
}
Expand Down
93 changes: 93 additions & 0 deletions pkg/mod/index_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package mod

import (
"os"
"path"
"sort"
"testing"
)
Expand All @@ -26,3 +28,94 @@ func TestKloneItemSorting(t *testing.T) {
}
}
}

func Test_editKloneFile(t *testing.T) {
tests := []struct {
name string
initial string
modifyFn func(*kloneFile) error
expected string
expectErr bool
}{
{
name: "Preserve comments",
initial: `# Test comment1
# Test comment2
targets:
target1:
- folder_name: Folder A
repo_url: https://github.com/repo1
repo_ref: main
repo_hash: abc123
repo_path: path/to/repo1
`,
modifyFn: func(kf *kloneFile) error {
return nil
},
expected: `# Test comment1
# Test comment2
targets:
target1:
- folder_name: Folder A
repo_url: https://github.com/repo1
repo_ref: main
repo_hash: abc123
repo_path: path/to/repo1
`,
expectErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tempDirPath := t.TempDir()

// Create the file and write the initial contents
{
tempFile, err := os.Create(path.Join(tempDirPath, kloneFileName))
if err != nil {
t.Fatalf("Failed to create temporary file: %v", err)
}

// Write the initial content to the temporary file
_, err = tempFile.WriteString(tt.initial)
if err != nil {
t.Fatalf("Failed to write initial content to temporary file: %v", err)
}

// Close the temporary file
err = tempFile.Close()
if err != nil {
t.Fatalf("Failed to close temporary file: %v", err)
}
}

// Create a WorkDir instance with the path to the temporary file
workDir := WorkDir(tempDirPath)

// Call the editKloneFile function with the modifyFn
err := workDir.editKloneFile(tt.modifyFn)

// Check if an error is expected
if tt.expectErr && err == nil {
t.Errorf("Expected an error, but got nil")
} else if !tt.expectErr && err != nil {
t.Errorf("Expected no error, but got: %v", err)
}

// Read the new file contents and compare with the expected contents
{
// Read the content of the modified file
modifiedContent, err := os.ReadFile(path.Join(tempDirPath, kloneFileName))
if err != nil {
t.Fatalf("Failed to read modified content: %v", err)
}

// Compare the modified content with the expected content
if string(modifiedContent) != tt.expected {
t.Errorf("Expected modified content:\n%s\n\nBut got:\n%s", tt.expected, modifiedContent)
}
}
})
}
}
24 changes: 24 additions & 0 deletions test-e2e.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

set -e

go build -o ./dist/klone_test .
klone_binary=$(realpath ./dist/klone_test)

# Test that the sync command works for the example directory
pushd ./example
"$klone_binary" sync
popd

# Create a temp directory and test that the init, add, and sync commands work
temp_dir=$(mktemp -d)
trap '{ rm -rf "$temp_dir"; echo "> Deleted temp dir $temp_dir"; }' EXIT
pushd "$temp_dir"
"$klone_binary" init
mkdir -p a/b
touch a/SHOULD_NOT_BE_DELETED
touch a/b/SHOULD_BE_DELETED
"$klone_binary" add a/b c/d https://github.com/cert-manager/community.git main logo
"$klone_binary" sync
tree -a
popd
7 changes: 0 additions & 7 deletions test.sh

This file was deleted.