Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use a unique exit code to indicate that no lockfiles could be found in the given args #138

Merged
merged 3 commits into from
Jul 9, 2022

Conversation

G-Rath
Copy link
Owner

@G-Rath G-Rath commented Jun 26, 2022

This makes it easier for tools to know when the detector didn't find anything to parse vs anything else - this is an important situation because this way tools can just point the detector at a directory without knowing if there will be anything for the detector to be able to parse.

@G-Rath G-Rath changed the title fix: use a unique exit code to indicate that no lockfiles could be found in the given args feat: use a unique exit code to indicate that no lockfiles could be found in the given args Jun 26, 2022
@G-Rath
Copy link
Owner Author

G-Rath commented Jun 30, 2022

So my gut is telling me there's a bug in the current implementation somewhere that means we'll return the wrong exit code in at least one situation, but after refactoring to be what I think is more accurate it didn't change any of the test results even after I messed around with creating files and folders as root (which are the main code paths that are not being covered in this section), so am just going to stick with what I have for now.

here's the patch though, just in case
Index: main.go
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/main.go b/main.go
--- a/main.go	(revision 7d48d3926610ed37588df4fb3fc98e140b153207)
+++ b/main.go	(date 1656620576613)
@@ -258,6 +258,8 @@
 	lockfiles := make([]string, 0, 1)
 	file, err := os.Open(pathToLockOrDirectory)
 
+	isDirWithoutLockfiles := false
+
 	if err == nil {
 		info, err := file.Stat()
 
@@ -266,6 +268,8 @@
 				dirs, err := file.ReadDir(-1)
 
 				if err == nil {
+					isDirWithoutLockfiles = true
+
 					for _, dir := range dirs {
 						if dir.IsDir() {
 							continue
@@ -275,6 +279,8 @@
 							if p, _ := lockfile.FindParser(dir.Name(), parseAs); p == nil {
 								continue
 							}
+
+							isDirWithoutLockfiles = false
 						}
 
 						lockfiles = append(lockfiles, path.Join(pathToLockOrDirectory, dir.Name()))
@@ -294,7 +300,7 @@
 		return lockfiles[i] < lockfiles[j]
 	})
 
-	return lockfiles, err != nil
+	return lockfiles, isDirWithoutLockfiles
 }
 
 func findAllLockfiles(r *reporter.Reporter, pathsToCheck []string, parseAs string) ([]string, bool) {
@@ -304,13 +310,13 @@
 		return []string{"-"}, false
 	}
 
-	errored := false
+	allEmptyDirs := true
 
 	for _, pathToLockOrDirectory := range pathsToCheck {
-		lps, erred := findLockfiles(r, pathToLockOrDirectory, parseAs)
+		lps, isEmptyDir := findLockfiles(r, pathToLockOrDirectory, parseAs)
 
-		if erred {
-			errored = true
+		if !isEmptyDir {
+			allEmptyDirs = false
 		}
 
 		for _, p := range lps {
@@ -318,7 +324,7 @@
 		}
 	}
 
-	return paths, errored
+	return paths, allEmptyDirs
 }
 
 func parseLockfile(pathToLock string, parseAs string, args []string) (lockfile.Lockfile, error) {
@@ -569,7 +575,7 @@
 		}
 	}
 
-	pathsToLocks, errored := findAllLockfiles(r, cli.Args(), *parseAs)
+	pathsToLocks, allEmptyDirs := findAllLockfiles(r, cli.Args(), *parseAs)
 
 	if len(pathsToLocks) == 0 {
 		r.PrintError(
@@ -579,7 +585,7 @@
 		// being provided with at least one path and not hitting an error on any of those
 		// paths means everything was valid, we just didn't find any parsable lockfiles
 		// in any of the directories
-		if len(cli.Args()) > 0 && !errored {
+		if len(cli.Args()) > 0 && allEmptyDirs {
 			// so we want to use a specific exit code to represent this state
 			return 128
 		}

@G-Rath G-Rath force-pushed the adjust-exit-code branch 2 times, most recently from 4c2ec2c to 93864f9 Compare July 5, 2022 19:46
@G-Rath G-Rath merged commit 2009ec2 into main Jul 9, 2022
@G-Rath G-Rath deleted the adjust-exit-code branch July 9, 2022 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant