Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduces Reader into k6deps.Source #69

Merged
merged 1 commit into from
Feb 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions archive_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k6deps

import (
"os"
"path/filepath"
"testing"

Expand Down Expand Up @@ -30,3 +31,23 @@ func Test_analyzeArchive(t *testing.T) {

require.Equal(t, expected.String(), actual.String())
}

func Test_analyzeArchive_Reader(t *testing.T) {
t.Parallel()

file, err := os.Open(filepath.Join("testdata", "archive.tar")) //nolint:forbidigo
require.NoError(t, err)
defer file.Close() //nolint:errcheck

opts := &Options{
Archive: Source{Reader: file},
}

actual, err := Analyze(opts)
require.NoError(t, err)

expected, err := Analyze(opts)
require.NoError(t, err)

require.Equal(t, expected.String(), actual.String())
}
57 changes: 37 additions & 20 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,41 @@ const EnvDependencies = "K6_DEPENDENCIES"
type Source struct {
// Name contains the name of the source (file, environment variable, etc.).
Name string
// Reader provides streaming access to the source content as an alternative to Contents.
Reader io.Reader
// Contents contains the content of the source (e.g. script)
Contents []byte
// Ignore disables automatic search and processing of that source.
Ignore bool
}

// IsEmpty returns true if the source is empty.
func (s *Source) IsEmpty() bool {
return len(s.Contents) == 0 && s.Reader == nil && len(s.Name) == 0
}

func (s *Source) getReader() (io.Reader, func() error, error) {
if s.Reader != nil {
return s.Reader, nil, nil
}

if len(s.Contents) > 0 {
return bytes.NewReader(s.Contents), nil, nil
}

fileName, err := filepath.Abs(s.Name)
if err != nil {
return nil, nil, err
}

file, err := os.Open(filepath.Clean(fileName)) //nolint:forbidigo
if err != nil {
return nil, nil, err
}

return file, file.Close, nil
}

// Options contains the parameters of the dependency analysis.
type Options struct {
// Script contains the properties of the k6 test script to be analyzed.
Expand Down Expand Up @@ -70,7 +99,7 @@ func (opts *Options) lookupEnv(key string) (string, bool) {
}

func loadSources(opts *Options) error {
if !opts.Archive.Ignore && (len(opts.Archive.Contents) > 0 || len(opts.Archive.Name) > 0) {
if !opts.Archive.Ignore && !opts.Archive.IsEmpty() {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, maybe a side-question for Ivan/Pablo, why the loadArchive is uniq the way we load, I mean it seems like if archive provided we completely ignore other paths, like manifest/env, could it cause any side effects? 🤔

return loadArchive(opts)
}

Expand Down Expand Up @@ -188,28 +217,16 @@ func findManifest(filename string) ([]byte, string, bool, error) {

//nolint:forbidigo
func loadArchive(opts *Options) error {
if opts.Archive.Ignore || (len(opts.Archive.Name) == 0 && len(opts.Archive.Contents) == 0) {
if opts.Archive.Ignore || opts.Archive.IsEmpty() {
return nil
}

var reader io.Reader

if len(opts.Archive.Contents) == 0 {
archivefile, err := filepath.Abs(opts.Archive.Name)
if err != nil {
return err
}

file, err := os.Open(filepath.Clean(archivefile))
if err != nil {
return err
}

defer file.Close() //nolint:errcheck

reader = file
} else {
reader = bytes.NewReader(opts.Archive.Contents)
reader, closer, err := opts.Archive.getReader()
if err != nil {
return err
}
if closer != nil {
defer closer() //nolint:errcheck
}

dir, err := os.MkdirTemp("", "k6deps-*")
Expand Down
Loading