From 9f9155e4a07a392fd15a9c12c5ad09f3b82258d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan-Otto=20Kr=C3=B6pke?= Date: Fri, 10 May 2024 11:48:16 +0200 Subject: [PATCH] fix: inconsistent output with multiple files MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jan-Otto Kröpke --- internal/provider/tar_archiver.go | 2 +- internal/provider/tar_archiver_test.go | 36 ++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/internal/provider/tar_archiver.go b/internal/provider/tar_archiver.go index ea4c4457..faf51e98 100644 --- a/internal/provider/tar_archiver.go +++ b/internal/provider/tar_archiver.go @@ -163,7 +163,7 @@ func (a *TarArchiver) ArchiveMultiple(content map[string][]byte) error { header := &tar.Header{ Name: filepath.ToSlash(filename), Size: int64(len(content[filename])), - ModTime: time.Now().Round(time.Second), + ModTime: time.Time{}, } if err := a.addContent(content[filename], header); err != nil { diff --git a/internal/provider/tar_archiver_test.go b/internal/provider/tar_archiver_test.go index 2a7ba238..71a5af58 100644 --- a/internal/provider/tar_archiver_test.go +++ b/internal/provider/tar_archiver_test.go @@ -168,6 +168,42 @@ func TestTarArchiver_Multiple(t *testing.T) { ensureTarContents(t, tarFilePath, content) } +func TestTarArchiver_Multiple_NoChange(t *testing.T) { + tarFilePath := filepath.Join(t.TempDir(), "archive-content.tar.gz") + + content := map[string][]byte{ + "file1.txt": []byte("This is file 1"), + "file2.txt": []byte("This is file 2"), + "file3.txt": []byte("This is file 3"), + } + + archiver := NewTarGzArchiver(tarFilePath) + if err := archiver.ArchiveMultiple(content); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + expectedContents, err := os.ReadFile(tarFilePath) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + time.Sleep(1 * time.Second) + + archiver = NewTarGzArchiver(tarFilePath) + if err := archiver.ArchiveMultiple(content); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + actualContents, err := os.ReadFile(tarFilePath) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if !bytes.Equal(expectedContents, actualContents) { + t.Fatalf("tar contents do not match, potentially a modified time issue") + } +} + func TestTarArchiver_Dir_With_Symlink_File(t *testing.T) { tarFilePath := filepath.Join(t.TempDir(), "archive-dir-with-symlink-file.tar.gz")