-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix windows upgrade path on install.ps1, build command-specific outpu…
…t formatters, rename --output value=buildId, add test for tgz creation
- Loading branch information
Showing
12 changed files
with
348 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package archive_test | ||
|
||
import ( | ||
"archive/tar" | ||
"compress/gzip" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"github.com/hathora/ci/internal/archive" | ||
) | ||
|
||
func Test_CreateTGZ(t *testing.T) { | ||
zapLogger, _ := zap.NewDevelopment() | ||
zap.ReplaceGlobals(zapLogger) | ||
tests := []struct { | ||
name string | ||
files map[string]string | ||
shouldFail bool | ||
}{ | ||
{ | ||
name: "simple", | ||
files: map[string]string{ | ||
"file1.txt": "This is file 1", | ||
"subdir/file2.txt": "This is file 2 in subdir", | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "nested", | ||
files: map[string]string{ | ||
"dir1/dir2/file3.txt": "This is file 3 in nested directory", | ||
"dir1/file4.txt": "This is file 4", | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "nested with dirs only", | ||
files: map[string]string{ | ||
"dir3/": "", | ||
"dir3/dir4/": "", | ||
"dir3/dir4/file5.txt": "This is file 5 in nested empty directory", | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "special characters in filenames", | ||
files: map[string]string{ | ||
"file with spaces.txt": "This is a file with spaces", | ||
"file-with-üñîçødé.txt": "This file has unicode characters", | ||
}, | ||
shouldFail: false, | ||
}, | ||
{ | ||
name: "empty", | ||
files: map[string]string{}, | ||
shouldFail: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
srcFolder, err := os.MkdirTemp("", "testsrc") | ||
require.NoError(t, err) | ||
t.Cleanup(func() { | ||
os.RemoveAll(srcFolder) | ||
}) | ||
|
||
for path, content := range tt.files { | ||
fullPath := filepath.Join(srcFolder, path) | ||
if strings.HasSuffix(path, "/") { | ||
require.NoError(t, os.MkdirAll(fullPath, os.ModePerm)) | ||
} else { | ||
err := os.MkdirAll(filepath.Dir(fullPath), os.ModePerm) | ||
require.NoError(t, err) | ||
err = os.WriteFile(fullPath, []byte(content), 0644) | ||
require.NoError(t, err) | ||
} | ||
} | ||
|
||
archivePath, err := archive.CreateTGZ(srcFolder) | ||
if tt.shouldFail { | ||
assert.Error(err) | ||
return | ||
} else { | ||
assert.NoError(err) | ||
} | ||
t.Cleanup(func() { | ||
os.Remove(archivePath) | ||
}) | ||
|
||
file, err := os.Open(archivePath) | ||
assert.NoError(err) | ||
t.Cleanup(func() { | ||
file.Close() | ||
}) | ||
|
||
gzipReader, err := gzip.NewReader(file) | ||
assert.NoError(err) | ||
t.Cleanup(func() { | ||
gzipReader.Close() | ||
}) | ||
|
||
tarReader := tar.NewReader(gzipReader) | ||
|
||
archivedFiles := make(map[string]string) | ||
for { | ||
header, err := tarReader.Next() | ||
if err == io.EOF { | ||
break | ||
} | ||
assert.NoError(err) | ||
|
||
if header.Typeflag == tar.TypeReg { | ||
content, err := io.ReadAll(tarReader) | ||
assert.NoError(err) | ||
archivedFiles[header.Name] = string(content) | ||
} | ||
} | ||
|
||
for path, expectedContent := range tt.files { | ||
if strings.HasSuffix(path, "/") { | ||
continue // Skip directories | ||
} | ||
content, found := archivedFiles[path] | ||
assert.True(found, "Expected file %s not found in archive", path) | ||
assert.Equal(expectedContent, content, "File content mismatch for %s", path) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.