Skip to content

Commit

Permalink
PIP resolution; fix windows lockfile format (#220)
Browse files Browse the repository at this point in the history
* Add failing format test with CRLF

* Add fix for windows newline in pip parser
  • Loading branch information
filip-debricked authored Apr 4, 2024
1 parent b9bd193 commit db811e6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
13 changes: 9 additions & 4 deletions internal/resolution/pm/pip/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ func (j *Job) getCouldNotFindVersionErrorDocumentation(cmdError job.IError) stri

func (j *Job) writeLockContent() job.IError {
status := "generating lock file"
var replacer = strings.NewReplacer("\r\n", "\n") // replace CRLF (Windows newline) with just LF (Unix newline)
j.SendStatus(status)
catCmdOutput, cmdErr := j.runCatCmd()
if cmdErr != nil {
Expand Down Expand Up @@ -248,16 +249,20 @@ func (j *Job) writeLockContent() job.IError {
defer closeFile(j, lockFile)

var fileContents []string
fileContents = append(fileContents, string(catCmdOutput))
fileContents = append(fileContents, replacer.Replace(string(catCmdOutput)))
fileContents = append(fileContents, lockFileDelimiter)
fileContents = append(fileContents, string(listCmdOutput))
fileContents = append(fileContents, replacer.Replace(string(listCmdOutput)))
fileContents = append(fileContents, lockFileDelimiter)
fileContents = append(fileContents, string(ShowCmdOutput))
fileContents = append(fileContents, replacer.Replace(string(ShowCmdOutput)))

res := []byte(strings.Join(fileContents, "\n"))

status = "writing lock file"
j.SendStatus(status)
err = j.fileWriter.Write(lockFile, res)
err = j.fileWriter.Write(
lockFile,
res,
)
if err != nil {
cmdErr = util.NewPMJobError(err.Error())
cmdErr.SetStatus(status)
Expand Down
14 changes: 14 additions & 0 deletions internal/resolution/pm/pip/job_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,20 @@ func TestRun(t *testing.T) {
assert.Equal(t, string(res), string(fileWriterMock.Contents))
}

func TestRunCRLFFiles(t *testing.T) {

fileWriterMock := &writerTestdata.FileWriterMock{}
cmdFactoryMock := testdata.NewCRLFEchoCmdFactory()
j := NewJob("file", true, cmdFactoryMock, fileWriterMock, pipCleaner{})

go jobTestdata.WaitStatus(j)
j.Run()

assert.False(t, j.Errors().HasError())
fmt.Println(string(fileWriterMock.Contents))
assert.False(t, strings.Contains(string(fileWriterMock.Contents), "\r\n"))
}

func TestRunInstall(t *testing.T) {
cmdFactoryMock := testdata.NewEchoCmdFactory()
fileWriterMock := &writerTestdata.FileWriterMock{}
Expand Down
48 changes: 48 additions & 0 deletions internal/resolution/pm/pip/testdata/crlf_cmd_factory_mock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package testdata

import (
"os/exec"
)

type CmdFactoryMockCRLF struct {
CreateVenvCmdName string
MakeCreateVenvErr error
InstallCmdName string
MakeInstallErr error
CatCmdName string
MakeCatErr error
ListCmdName string
MakeListErr error
ShowCmdName string
MakeShowErr error
}

func NewCRLFEchoCmdFactory() CmdFactoryMockCRLF {
return CmdFactoryMockCRLF{
CreateVenvCmdName: "echo",
InstallCmdName: "echo",
CatCmdName: "echo",
ListCmdName: "echo",
ShowCmdName: "echo",
}
}

func (f CmdFactoryMockCRLF) MakeCreateVenvCmd(file string) (*exec.Cmd, error) {
return exec.Command(f.CreateVenvCmdName, file), f.MakeCreateVenvErr
}

func (f CmdFactoryMockCRLF) MakeInstallCmd(command string, file string) (*exec.Cmd, error) {
return exec.Command(f.InstallCmdName, file), f.MakeInstallErr
}

func (f CmdFactoryMockCRLF) MakeListCmd(command string) (*exec.Cmd, error) {
return exec.Command(f.ListCmdName, "Package Version Editable project location\r\n----------------------------- ------------ ------------------------------------------------------\r\nFlask 2.0.3"), f.MakeListErr
}

func (f CmdFactoryMockCRLF) MakeCatCmd(file string) (*exec.Cmd, error) {
return exec.Command(f.CatCmdName, "Flask==2.1.5\r\n"), f.MakeCatErr
}

func (f CmdFactoryMockCRLF) MakeShowCmd(command string, list []string) (*exec.Cmd, error) {
return exec.Command(f.ShowCmdName, "Name: Flask\r\nVersion: 2.1.2\r\nSummary: A simple framework for building complex web applications.\r\nHome-page: https://palletsprojects.com/p/flask\r\nAuthor: Armin Ronacher\r\nAuthor-email: [email protected]\r\nLicense: BSD-3-Clause\r\nLocation: /path/to/site-packages\r\nRequires: click, importlib-metadata, itsdangerous, Jinja2, Werkzeug\r\nRequired-by: Flask-Script, Flask-Compress, Flask-Bcrypt\r\n"), f.MakeShowErr
}

0 comments on commit db811e6

Please sign in to comment.