Skip to content

Commit

Permalink
Merge pull request #2012 from posit-dev/mm-python-comments
Browse files Browse the repository at this point in the history
Handle indented comments in requirements.txt
  • Loading branch information
mmarchetti authored Jul 23, 2024
2 parents ecd6c90 + ae5f30e commit dfe73b2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion internal/inspect/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io/fs"
"os"
"regexp"
"slices"
"strings"

Expand Down Expand Up @@ -180,8 +181,9 @@ func (i *defaultPythonInspector) ReadRequirementsFile(path util.AbsolutePath) ([
return nil, err
}
lines := strings.Split(string(content), "\n")
commentRE := regexp.MustCompile(`^\s*#`)
lines = slices.DeleteFunc(lines, func(line string) bool {
return line == "" || strings.HasPrefix(line, "#")
return line == "" || commentRE.MatchString(line)
})
return lines, nil
}
Expand Down
15 changes: 15 additions & 0 deletions internal/inspect/python_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,18 @@ func (s *PythonSuite) TestScanRequirements() {
s.Equal(pythonPath.String(), python)
scanner.AssertExpectations(s.T())
}

func (s *PythonSuite) TestReadRequirementsFile() {
log := logging.New()
i := NewPythonInspector(s.cwd, util.Path{}, log)

filePath := s.cwd.Join("requirements.txt")
filePath.WriteFile([]byte("# leading comment\nnumpy==1.26.1\npandas\n # indented comment\n"), 0777)

reqs, err := i.ReadRequirementsFile(filePath)
s.NoError(err)
s.Equal([]string{
"numpy==1.26.1",
"pandas",
}, reqs)
}

0 comments on commit dfe73b2

Please sign in to comment.