diff --git a/commands/curation/curationaudit.go b/commands/curation/curationaudit.go index 38c0feb8b..c91725e5a 100644 --- a/commands/curation/curationaudit.go +++ b/commands/curation/curationaudit.go @@ -94,6 +94,8 @@ const ( errorTemplateUnsupportedTech = "It looks like this project uses '%s' to download its dependencies. " + "This package manager however isn't supported by this command." + errorNothingAudited = "no supported package manager was recognised in any working directory, so nothing was audited. " + + "Check the working directories, or pass --requirements-file for a custom pip file name." 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." @@ -311,7 +313,9 @@ type CurationAuditCommand struct { dockerImageName string huggingFaceModel string // hfProjectNameHint is the collision-free HF root-node name for the current working dir. - hfProjectNameHint string + hfProjectNameHint string + // detectedTechCount counts technologies detected across all working dirs; zero fails the run. + detectedTechCount int includeCachedPackages bool mvnIncludePluginDeps bool scriptPath string @@ -495,6 +499,9 @@ func (ca *CurationAuditCommand) Run() (err error) { if ca.Progress() != nil { err = errors.Join(err, ca.Progress().Quit()) } + if err == nil && ca.detectedTechCount == 0 { + err = errorutils.CheckErrorf(errorNothingAudited) + } // Print after the spinner has stopped so messages are not overwritten by ANSI clear codes. for _, w := range ca.pendingWarnings { log.Warn(w) @@ -780,7 +787,7 @@ 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 = dedupeDotnetFromNuget(techs) @@ -830,6 +837,7 @@ func (ca *CurationAuditCommand) doCurateAudit(results map[string]*CurationReport return err } techs := ca.techsToAudit() + ca.detectedTechCount += len(techs) for _, tech := range techs { supportedFunc, ok := supportedTech[techutils.Technology(tech)] if !ok { diff --git a/commands/curation/curationaudit_test.go b/commands/curation/curationaudit_test.go index 12924243c..2712a77e7 100644 --- a/commands/curation/curationaudit_test.go +++ b/commands/curation/curationaudit_test.go @@ -2266,7 +2266,8 @@ func TestRunGeneratesAuditIdForHttpDetails(t *testing.T) { defer func() { require.NoError(t, os.Chdir(origWd)) }() ca := NewCurationAuditCommand().SetWorkingDirs([]string{curWd}) - require.NoError(t, ca.Run()) + // An empty directory audits nothing, so Run fails; the audit ID is still generated. + require.ErrorContains(t, ca.Run(), "nothing was audited") require.NotEmpty(t, ca.auditId) _, parseErr := uuid.Parse(ca.auditId) @@ -4759,6 +4760,74 @@ 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") +} + +// TestCurationAuditFailsWhenNothingIsAudited: a run that recognises no package manager +// must fail, so a CI gate cannot pass with nothing checked. +func TestCurationAuditFailsWhenNothingIsAudited(t *testing.T) { + t.Setenv("HOME", t.TempDir()) + t.Setenv("USERPROFILE", t.TempDir()) + projectDir := t.TempDir() + t.Chdir(projectDir) + + ca := NewCurationAuditCommand() + ca.SetWorkingDirs([]string{projectDir}) + + err := ca.Run() + + require.Error(t, err, "a run that audits nothing must not succeed") + assert.Contains(t, err.Error(), "nothing was audited") +} + +// TestRunSucceedsWhenOneWorkingDirIsAudited: a run with one audited directory and one empty +// directory must still succeed; only a run that audits nothing fails. +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