Skip to content

Commit

Permalink
fix e2e resolve tests and pip prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
emilwareus committed Oct 26, 2023
1 parent 1783909 commit 163b7a9
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dist/
/gomod.debricked.lock
/.env
test/resolve/testdata/pip/requirements.txt.venv/
test/resolve/testdata/pip/requirements.txt.debricked.lock
test/resolve/testdata/pip/requirements.txt.pip.debricked.lock
internal/cmd/scan/testdata/npm/yarn.lock
internal/resolution/pm/gradle/.gradle-init-script.debricked.groovy
test/resolve/testdata/npm/yarn.lock
Expand Down
4 changes: 2 additions & 2 deletions internal/resolution/pm/pip/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

const (
lockFileExtension = "pip.debricked.lock"
lockFileExtension = ".pip.debricked.lock"
pip = "pip"
lockFileDelimiter = "***"
)
Expand Down Expand Up @@ -115,7 +115,7 @@ func (j *Job) writeLockContent() error {
return err
}

lockFileName := fmt.Sprintf(".%s%s", filepath.Base(j.GetFile()), lockFileExtension)
lockFileName := fmt.Sprintf("%s%s", filepath.Base(j.GetFile()), lockFileExtension)
lockFile, err := j.fileWriter.Create(util.MakePathFromManifestFile(j.GetFile(), lockFileName))
if err != nil {
return err
Expand Down
63 changes: 45 additions & 18 deletions test/resolve/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,60 @@ package resolve
import (
"os"
"path/filepath"
"strings"
"testing"

"github.com/debricked/cli/internal/cmd/resolve"
"github.com/debricked/cli/internal/wire"
"github.com/stretchr/testify/assert"
)

func removeLines(input, prefix string) string {
lines := strings.Split(input, "\n")
var result []string

for _, line := range lines {
if !strings.HasPrefix(line, prefix) {
result = append(result, line)
}
}

return strings.Join(result, "\n")
}

func TestResolves(t *testing.T) {
cases := []struct {
name string
manifestFile string
lockFileName string
expectedFile string
name string
manifestFile string
lockFileName string
expectedFile string
packageManager string
}{
{
name: "basic package.json",
manifestFile: "testdata/npm/package.json",
lockFileName: "yarn.lock",
expectedFile: "testdata/npm/yarn-expected.lock",
name: "basic package.json",
manifestFile: "testdata/npm/package.json",
lockFileName: "yarn.lock",
expectedFile: "testdata/npm/yarn-expected.lock",
packageManager: "yarn",
},
{
name: "basic requirements.txt",
manifestFile: "testdata/pip/requirements.txt",
lockFileName: "requirements.txt.debricked.lock",
expectedFile: "testdata/pip/expected.lock",
name: "basic requirements.txt",
manifestFile: "testdata/pip/requirements.txt",
lockFileName: "requirements.txt.pip.debricked.lock",
expectedFile: "testdata/pip/expected.lock",
packageManager: "pip",
},
{
name: "basic .csproj",
manifestFile: "testdata/nuget/basic.csproj",
lockFileName: "packages.lock.json",
expectedFile: "testdata/nuget/packages-expected.lock.json",
name: "basic .csproj",
manifestFile: "testdata/nuget/basic.csproj",
lockFileName: "packages.lock.json",
expectedFile: "testdata/nuget/packages-expected.lock.json",
packageManager: "nuget",
},
}

for _, c := range cases {
for _, cT := range cases {
c := cT
t.Run(c.name, func(t *testing.T) {
resolveCmd := resolve.NewResolveCmd(wire.GetCliContainer().Resolver())
lockFileDir := filepath.Dir(c.manifestFile)
Expand All @@ -53,8 +72,16 @@ func TestResolves(t *testing.T) {

expectedFileContents, fileErr := os.ReadFile(c.expectedFile)
assert.NoError(t, fileErr)
expectedString := string(expectedFileContents)
actualString := string(lockFileContents)

if c.packageManager == "pip" {
// Remove locations as it is dependent on the machine
expectedString = removeLines(expectedString, "Location: ")
actualString = removeLines(actualString, "Location: ")

assert.Equal(t, string(expectedFileContents), string(lockFileContents))
}
assert.Equal(t, expectedString, actualString)
})
}
}
6 changes: 6 additions & 0 deletions test/resolve/testdata/pip/expected.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
pandas==1.5.1
numpy==1.24.2
pip==22.0.4
python-dateutil==2.8.2
pytz==2022.7.1
setuptools==58.1.0
six==1.16.0
# comment

***
Expand Down
6 changes: 6 additions & 0 deletions test/resolve/testdata/pip/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
pandas==1.5.1
numpy==1.24.2
pip==22.0.4
python-dateutil==2.8.2
pytz==2022.7.1
setuptools==58.1.0
six==1.16.0
# comment

0 comments on commit 163b7a9

Please sign in to comment.