From 7991c010396d43c086a5e4cd4e798ba3e66c0fcc Mon Sep 17 00:00:00 2001 From: Yusuf Birader Date: Mon, 14 Aug 2023 13:42:35 +0100 Subject: [PATCH] add method deflate to header --- main.go | 1 + main_test.go | 103 +++++++++++++++++++++++++++++++-------------------- 2 files changed, 63 insertions(+), 41 deletions(-) diff --git a/main.go b/main.go index 9792090..65fac22 100644 --- a/main.go +++ b/main.go @@ -295,6 +295,7 @@ func (a *Archiver) constructHeader(file *File) error { return err } } + header.Method = zip.Deflate file.Header = header return nil } diff --git a/main_test.go b/main_test.go index 94856fc..de97f24 100644 --- a/main_test.go +++ b/main_test.go @@ -137,66 +137,87 @@ func TestCompressToBuffer(t *testing.T) { } func TestFileWriter(t *testing.T) { - t.Run("constructs zip file header for a single compressed file where path to file is absolute", func(t *testing.T) { - archive, cleanup := createTempArchive(t, archivePath) - defer cleanup() + t.Run("writes correct header", func(t *testing.T) { + t.Run("constructs zip file header for a single compressed file where path to file is absolute", func(t *testing.T) { + archive, cleanup := createTempArchive(t, archivePath) + defer cleanup() - archiver, err := NewArchiver(archive) - assert.NoError(t, err) + archiver, err := NewArchiver(archive) + assert.NoError(t, err) - info := getFileInfo(t, helloTxtFileFixture) + info := getFileInfo(t, helloTxtFileFixture) - absPath, err := filepath.Abs(helloTxtFileFixture) - assert.NoError(t, err) - file := File{Path: absPath, Info: info} + absPath, err := filepath.Abs(helloTxtFileFixture) + assert.NoError(t, err) + file := File{Path: absPath, Info: info} - archiver.constructHeader(&file) + archiver.constructHeader(&file) - want := &zip.FileHeader{ - Name: "hello.txt", - } + want := &zip.FileHeader{ + Name: "hello.txt", + } - assert.Equal(t, want.Name, file.Header.Name) - }) + assert.Equal(t, want.Name, file.Header.Name) + }) - t.Run("constructs zip file header for a single compressed file where path to file is relative", func(t *testing.T) { - archive, cleanup := createTempArchive(t, archivePath) - defer cleanup() + t.Run("constructs zip file header for a single compressed file where path to file is relative", func(t *testing.T) { + archive, cleanup := createTempArchive(t, archivePath) + defer cleanup() - archiver, err := NewArchiver(archive) - assert.NoError(t, err) + archiver, err := NewArchiver(archive) + assert.NoError(t, err) - info := getFileInfo(t, helloTxtFileFixture) - file := File{Path: helloTxtFileFixture, Info: info} + info := getFileInfo(t, helloTxtFileFixture) + file := File{Path: helloTxtFileFixture, Info: info} - archiver.constructHeader(&file) + archiver.constructHeader(&file) - want := &zip.FileHeader{ - Name: "hello.txt", - } + want := &zip.FileHeader{ + Name: "hello.txt", + } - assert.Equal(t, want.Name, file.Header.Name) - }) + assert.Equal(t, want.Name, file.Header.Name) + }) - t.Run("constructs a zip file header of file where path to root directory is absolute", func(t *testing.T) { - archive, cleanup := createTempArchive(t, archivePath) - defer cleanup() + t.Run("constructs a zip file header of file where path to root directory is absolute", func(t *testing.T) { + archive, cleanup := createTempArchive(t, archivePath) + defer cleanup() - archiver, err := NewArchiver(archive) - assert.NoError(t, err) + archiver, err := NewArchiver(archive) + assert.NoError(t, err) - archiver.setRootDir(helloDirectoryFixture) + archiver.setRootDir(helloDirectoryFixture) - info := getFileInfo(t, filepath.Join(archiver.root, "/nested/hello.md")) - file := File{Path: filepath.Join(archiver.root, "/nested/hello.md"), Info: info} + info := getFileInfo(t, filepath.Join(archiver.root, "/nested/hello.md")) + file := File{Path: filepath.Join(archiver.root, "/nested/hello.md"), Info: info} - archiver.constructHeader(&file) + archiver.constructHeader(&file) - want := &zip.FileHeader{ - Name: "nested/hello.md", - } + want := &zip.FileHeader{ + Name: "nested/hello.md", + } + + assert.Equal(t, want.Name, file.Header.Name) + }) + + t.Run("sets correct method header", func(t *testing.T) { + archive, cleanup := createTempArchive(t, archivePath) + defer cleanup() + + archiver, err := NewArchiver(archive) + assert.NoError(t, err) + + archiver.setRootDir(helloDirectoryFixture) - assert.Equal(t, want.Name, file.Header.Name) + info := getFileInfo(t, filepath.Join(archiver.root, "/nested/hello.md")) + file := File{Path: filepath.Join(archiver.root, "/nested/hello.md"), Info: info} + + archiver.constructHeader(&file) + + want := zip.Deflate + + assert.Equal(t, want, file.Header.Method) + }) }) }