Skip to content

Commit

Permalink
refactor: inject context
Browse files Browse the repository at this point in the history
  • Loading branch information
ybirader committed Sep 6, 2023
1 parent 87d7cd6 commit 909040f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 11 deletions.
6 changes: 3 additions & 3 deletions archiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ func NewArchiver(archive *os.File) (*Archiver, error) {
return a, nil
}

func (a *Archiver) Archive(filePaths []string) error {
a.fileProcessPool.Start(context.Background())
a.fileWriterPool.Start(context.Background())
func (a *Archiver) Archive(ctx context.Context, filePaths []string) error {
a.fileProcessPool.Start(ctx)
a.fileWriterPool.Start(ctx)

for _, path := range filePaths {
info, err := os.Lstat(path)
Expand Down
15 changes: 8 additions & 7 deletions archiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package pzip
import (
"archive/zip"
"bytes"
"context"
"encoding/binary"
"fmt"
"path/filepath"
Expand All @@ -29,7 +30,7 @@ func TestArchive(t *testing.T) {

archiver, err := NewArchiver(archive)
assert.NoError(t, err)
archiver.Archive([]string{helloTxtFileFixture})
archiver.Archive(context.Background(), []string{helloTxtFileFixture})
archiver.Close()

archiveReader := testutils.GetArchiveReader(t, archive.Name())
Expand All @@ -52,7 +53,7 @@ func TestArchive(t *testing.T) {

archiver, err := NewArchiver(archive)
assert.NoError(t, err)
archiver.Archive([]string{helloTxtFileFixture})
archiver.Archive(context.Background(), []string{helloTxtFileFixture})
archiver.Close()

archiveReader := testutils.GetArchiveReader(t, archive.Name())
Expand All @@ -74,7 +75,7 @@ func TestArchive(t *testing.T) {

archiver, err := NewArchiver(archive)
assert.NoError(t, err)
archiver.Archive([]string{helloTxtFileFixture, helloMarkdownFileFixture})
archiver.Archive(context.Background(), []string{helloTxtFileFixture, helloMarkdownFileFixture})
archiver.Close()

archiveReader := testutils.GetArchiveReader(t, archive.Name())
Expand All @@ -89,7 +90,7 @@ func TestArchive(t *testing.T) {

archiver, err := NewArchiver(archive)
assert.NoError(t, err)
err = archiver.Archive([]string{helloDirectoryFixture})
err = archiver.Archive(context.Background(), []string{helloDirectoryFixture})
assert.NoError(t, err)
archiver.Close()

Expand All @@ -105,9 +106,9 @@ func TestArchive(t *testing.T) {

archiver, err := NewArchiver(archive)
assert.NoError(t, err)
err = archiver.Archive([]string{helloTxtFileFixture})
err = archiver.Archive(context.Background(), []string{helloTxtFileFixture})
assert.NoError(t, err)
err = archiver.Archive([]string{helloMarkdownFileFixture})
err = archiver.Archive(context.Background(), []string{helloMarkdownFileFixture})
assert.NoError(t, err)
archiver.Close()

Expand Down Expand Up @@ -222,6 +223,6 @@ func BenchmarkArchive(b *testing.B) {
b.ResetTimer()

for i := 0; i < b.N; i++ {
archiver.Archive([]string{helloTxtFileFixture, helloMarkdownFileFixture})
archiver.Archive(context.Background(), []string{helloTxtFileFixture, helloMarkdownFileFixture})
}
}
3 changes: 2 additions & 1 deletion cli.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package pzip

import (
"context"
"os"

"github.com/pkg/errors"
Expand All @@ -24,7 +25,7 @@ func (c *CLI) Archive() error {
}
defer archiver.Close()

err = archiver.Archive(c.Files)
err = archiver.Archive(context.Background(), c.Files)
if err != nil {
return errors.Wrapf(err, "ERROR: could not archive files")
}
Expand Down

0 comments on commit 909040f

Please sign in to comment.