diff --git a/commands/curation/curationaudit.go b/commands/curation/curationaudit.go index b96749607..956500d51 100644 --- a/commands/curation/curationaudit.go +++ b/commands/curation/curationaudit.go @@ -102,7 +102,6 @@ const ( errorTemplateUnsupportedTech = "It looks like this project uses '%s' to download its dependencies. " + "This package manager however isn't supported by this command." - WaiverRequestForbidden = "One or more policies blocking this package do not allow waiver requests." WaiverRequestApproved = "The waiver request was automatically granted; you can use this package.\nNOTE: The policy owner may review this waiver more thoroughly and contact you if issues are found." WaiverRequestPending = "A waiver request was opened for review, and the owner was notified.\nYou will receive an email with an update once the status changes." @@ -768,6 +767,32 @@ func promotePipToUvIn(techs []string, dir string) []string { return result } +// promotePipToUvForRequest keeps the pip signal from --requirements-file. When that file exists in +// the working dir, only project-local uv signals apply, as in 'jf audit'; a machine-global uv config +// must not override it. Otherwise it behaves as promotePipToUvIn. +func (ca *CurationAuditCommand) promotePipToUvForRequest(techs []string) []string { + if !ca.pipRequirementsFileExists() { + return promotePipToUvIn(techs, ca.OriginPath) + } + promoted := techutils.PromotePipToUv(techutils.ToTechnologies(techs), ca.OriginPath) + result := make([]string, 0, len(promoted)) + for _, t := range promoted { + result = append(result, t.String()) + } + return result +} + +// pipRequirementsFileExists reports whether --requirements-file names an existing file in the +// working dir. Absolute paths are not checked. +func (ca *CurationAuditCommand) pipRequirementsFileExists() bool { + file := ca.PipRequirementsFile() + if file == "" || ca.OriginPath == "" || filepath.IsAbs(file) { + return false + } + info, err := os.Stat(filepath.Join(ca.OriginPath, file)) + return err == nil && !info.IsDir() +} + // dedupeDotnetFromNuget drops Dotnet from techs, since Nuget alone already covers it. func dedupeDotnetFromNuget(techs []string) []string { return removeTech(techs, techutils.Dotnet.String()) @@ -799,9 +824,9 @@ func (ca *CurationAuditCommand) techsToAudit() []string { log.Debug(fmt.Sprintf("Docker image name '%s' was provided, running Docker curation audit.", ca.DockerImageName())) return []string{techutils.Docker.String()} default: - techs := promotePnpmWorkspaceMember(techutils.DetectedTechnologiesListForCurationAudit()) + techs := promotePnpmWorkspaceMember(techutils.DetectedTechnologiesListForCurationAuditWithRequirementsFile(ca.PipRequirementsFile())) techs = promoteYarnWorkspaceMember(techs) - techs = promotePipToUvIn(techs, ca.OriginPath) + techs = ca.promotePipToUvForRequest(techs) techs = dedupeDotnetFromNuget(techs) // Auto-discovery: if HF_ENDPOINT is set and .py/.ipynb files exist, append HF to the tech list. if os.Getenv("HF_ENDPOINT") != "" && hasPythonFiles(ca.OriginPath) { diff --git a/commands/curation/curationaudit_test.go b/commands/curation/curationaudit_test.go index 0db0fc843..f15a3a056 100644 --- a/commands/curation/curationaudit_test.go +++ b/commands/curation/curationaudit_test.go @@ -4813,6 +4813,79 @@ func TestPureUvProjectNotReportedAsPip(t *testing.T) { assert.NotContains(t, techs, techutils.Pip.String(), "must not also report pip for a plain uv-only project") } +// TestTechsToAuditRecognisesCustomRequirementsFile: jf ca must detect pip from the file +// named by --requirements-file, the same way jf audit does. +func TestTechsToAuditRecognisesCustomRequirementsFile(t *testing.T) { + // Isolate the home dir so a machine-global ~/.config/uv/uv.toml cannot promote pip to uv. + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", t.TempDir()) + projectDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(projectDir, "constraints.txt"), []byte("requests==2.31.0\n"), 0644)) + t.Chdir(projectDir) + + ca := NewCurationAuditCommand() + ca.SetPipRequirementsFile("constraints.txt") + ca.OriginPath = projectDir + + assert.Contains(t, ca.techsToAudit(), techutils.Pip.String(), "the file named by --requirements-file must be audited as pip") +} + +// A machine-global uv config must not turn a directory whose --requirements-file exists into uv. +func TestTechsToAuditRequirementsFileIgnoresGlobalUvConfig(t *testing.T) { + home := t.TempDir() + t.Setenv("HOME", home) + t.Setenv("USERPROFILE", home) + uvCfgDir := filepath.Join(home, ".config", "uv") + require.NoError(t, os.MkdirAll(uvCfgDir, 0o755)) + require.NoError(t, os.WriteFile(filepath.Join(uvCfgDir, "uv.toml"), []byte("[[index]]\nurl = \"https://example.com/simple\"\n"), 0o644)) + + projectDir := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(projectDir, "constraints.txt"), []byte("requests==2.31.0\n"), 0644)) + t.Chdir(projectDir) + + ca := NewCurationAuditCommand() + ca.SetPipRequirementsFile("constraints.txt") + ca.OriginPath = projectDir + + techs := ca.techsToAudit() + assert.Contains(t, techs, techutils.Pip.String(), "the requirements file must keep the directory as pip") + assert.NotContains(t, techs, techutils.Uv.String(), "the global uv config must not override the requirements file") +} + +// TestRunSucceedsWhenOneWorkingDirIsAudited: an empty directory alongside an audited one +// must not fail the run. +func TestRunSucceedsWhenOneWorkingDirIsAudited(t *testing.T) { + var tt testCase + for _, candidate := range getTestCasesForDoCurationAudit() { + if candidate.name == "python tree - one blocked package" { + tt = candidate + } + } + require.NotEmpty(t, tt.name, "the pip curation case must exist") + basePathToTests, err := filepath.Abs(TestDataDir) + require.NoError(t, err) + + cleanUpFlags := setCurationFlagsForTest(t) + defer cleanUpFlags() + mockServer, serverDetails := curationServer(t, tt.expectedBuildRequest, tt.expectedRequest, tt.requestToFail, tt.requestToError, tt.serveResources) + defer mockServer.Close() + cleanUp := createCurationTestEnv(t, basePathToTests, tt, serverDetails) + defer cleanUp() + require.NoError(t, os.Mkdir("empty-dir", 0755)) + + ca := NewCurationAuditCommand() + ca.SetIsCurationCmd(true) + ca.parallelRequests = 3 + ca.SetInsecureTls(tt.allowInsecureTls) + ca.SetIgnoreConfigFile(tt.shouldIgnoreConfigFile) + ca.SetWorkingDirs([]string{".", "empty-dir"}) + if defaultServer, confErr := config.GetDefaultServerConf(); confErr == nil { + ca.SetServerDetails(defaultServer) + } + + assert.NoError(t, ca.Run(), "a run where one directory is audited must succeed") +} + // TestTechsToAuditQueuesPep723HintForDeferredLogging: the PEP 723 hint must be queued in pendingWarnings. func TestTechsToAuditQueuesPep723HintForDeferredLogging(t *testing.T) { projectDir := t.TempDir() diff --git a/utils/techutils/techutils.go b/utils/techutils/techutils.go index 4584005b3..d1ab65d24 100644 --- a/utils/techutils/techutils.go +++ b/utils/techutils/techutils.go @@ -476,7 +476,13 @@ func DetectedTechnologiesList() (technologies []string) { return detectedTechnologiesListInPath(wd, false) } -// DetectedTechnologiesListForCurationAudit is the curation-audit variant of +// DetectedTechnologiesListForCurationAudit is DetectedTechnologiesListForCurationAuditWithRequirementsFile +// with no requirements file. +func DetectedTechnologiesListForCurationAudit() (technologies []string) { + return DetectedTechnologiesListForCurationAuditWithRequirementsFile("") +} + +// DetectedTechnologiesListForCurationAuditWithRequirementsFile is the curation-audit variant of // DetectedTechnologiesList that additionally re-routes any working directory // detected as Npm to Yarn whenever a parent directory is a yarn workspace // root that claims it. The promotion is curation-only by design — the @@ -484,21 +490,27 @@ func DetectedTechnologiesList() (technologies []string) { // commands ('jf audit', 'jf scan', etc.) keep the legacy npm-fallback // behaviour for yarn workspace members so this change cannot regress them. // +// pipRequirementsFile is the --requirements-file value; it is matched as jf audit matches it. +// // Unlike DetectedTechnologiesList, this does not log the result: the caller still has // more promotions to apply (pnpm/yarn, pip→uv) before the list is final, and logging // early could show a technology immediately superseded (e.g. "Detected: pip." right // before "...treating as uv."). // -// Used by doCurateAudit so 'jf ca --working-dirs=' +// Used by techsToAudit so 'jf ca --working-dirs=' // resolves through yarn (matching how the project's lockfile was produced) // instead of npm (which would synthesise a different dependency set and // produce curation answers that don't match what yarn would have resolved). -func DetectedTechnologiesListForCurationAudit() (technologies []string) { +func DetectedTechnologiesListForCurationAuditWithRequirementsFile(pipRequirementsFile string) (technologies []string) { wd, err := os.Getwd() if errorutils.CheckError(err) != nil { return } - detected, err := DetectTechnologiesDescriptors(wd, false, []string{}, map[Technology][]string{}, "") + requestedDescriptors := map[Technology][]string{} + if pipRequirementsFile != "" { + requestedDescriptors[Pip] = []string{pipRequirementsFile} + } + detected, err := DetectTechnologiesDescriptors(wd, false, []string{}, requestedDescriptors, "") if err != nil { return } diff --git a/utils/techutils/techutils_test.go b/utils/techutils/techutils_test.go index de6f05563..5ba2083ab 100644 --- a/utils/techutils/techutils_test.go +++ b/utils/techutils/techutils_test.go @@ -1278,6 +1278,38 @@ func TestPep723Hint_UvSibling_HintFires(t *testing.T) { assert.True(t, found, "the sibling script's inline deps are out of scope for the project's own audit") } +// TestDetectedTechnologiesListForCurationAuditWithRequirementsFile: a custom-named file +// named by the requirements flag must make the directory pip, as jf audit does. +func TestDetectedTechnologiesListForCurationAuditWithRequirementsFile(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(root, "constraints.txt"), []byte("requests==2.31.0\n"), 0644)) + + prevWd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(root)) + defer clientTests.ChangeDirAndAssert(t, prevWd) + + techs := DetectedTechnologiesListForCurationAuditWithRequirementsFile("constraints.txt") + + assert.Contains(t, techs, Pip.String(), "the file named by --requirements-file must make the directory pip") +} + +// TestDetectedTechnologiesListForCurationAuditWithoutRequirementsFile: without the flag a +// custom-named file is not a pip indicator, so nothing is detected. +func TestDetectedTechnologiesListForCurationAuditWithoutRequirementsFile(t *testing.T) { + root := t.TempDir() + require.NoError(t, os.WriteFile(filepath.Join(root, "constraints.txt"), []byte("requests==2.31.0\n"), 0644)) + + prevWd, err := os.Getwd() + require.NoError(t, err) + require.NoError(t, os.Chdir(root)) + defer clientTests.ChangeDirAndAssert(t, prevWd) + + techs := DetectedTechnologiesListForCurationAuditWithRequirementsFile("") + + assert.Empty(t, techs, "a custom-named file must not be detected as pip when the flag is unset") +} + func TestHasPep723ScriptMetadata(t *testing.T) { tests := []struct { name string