From ae5f30ed24a333a3b2e5b4e1326413f8bab4e033 Mon Sep 17 00:00:00 2001 From: Michael Marchetti Date: Tue, 23 Jul 2024 11:21:55 -0400 Subject: [PATCH] handle indented comments in requirements.txt --- internal/inspect/python.go | 4 +++- internal/inspect/python_test.go | 15 +++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/internal/inspect/python.go b/internal/inspect/python.go index b8862379e..3c08954c1 100644 --- a/internal/inspect/python.go +++ b/internal/inspect/python.go @@ -6,6 +6,7 @@ import ( "fmt" "io/fs" "os" + "regexp" "slices" "strings" @@ -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 } diff --git a/internal/inspect/python_test.go b/internal/inspect/python_test.go index 564adfabb..0c5673481 100644 --- a/internal/inspect/python_test.go +++ b/internal/inspect/python_test.go @@ -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) +}