diff --git a/.github/workflows/build.yml b/.github/workflows/tests.yml similarity index 54% rename from .github/workflows/build.yml rename to .github/workflows/tests.yml index 8929183..35692f0 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/tests.yml @@ -1,4 +1,4 @@ -name: build +name: tests on: pull_request: @@ -8,7 +8,7 @@ permissions: contents: read jobs: - build: + unit: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -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 diff --git a/example/klone.yaml b/example/klone.yaml index a50991e..9b53b61 100644 --- a/example/klone.yaml +++ b/example/klone.yaml @@ -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 diff --git a/pkg/mod/index.go b/pkg/mod/index.go index 903a03d..248521f 100644 --- a/pkg/mod/index.go +++ b/pkg/mod/index.go @@ -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 } diff --git a/pkg/mod/index_test.go b/pkg/mod/index_test.go index ea8fad3..9ad6380 100644 --- a/pkg/mod/index_test.go +++ b/pkg/mod/index_test.go @@ -1,6 +1,8 @@ package mod import ( + "os" + "path" "sort" "testing" ) @@ -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) + } + } + }) + } +} diff --git a/test-e2e.sh b/test-e2e.sh new file mode 100755 index 0000000..060a93e --- /dev/null +++ b/test-e2e.sh @@ -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 diff --git a/test.sh b/test.sh deleted file mode 100755 index ae1522d..0000000 --- a/test.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -set -e - -go build -o ./dist/klone_test . - -cd ./example && ../dist/klone_test sync