From b96a95971086ac372d67f76a9f3969c43a4aa2d1 Mon Sep 17 00:00:00 2001 From: "Thomas M. Donahue" Date: Mon, 11 Mar 2024 11:28:00 -0400 Subject: [PATCH] #16: Fix alt root lookup by using lookup function. Also refactors to new function name to reflect usage --- drushlauncher/drushlauncher.go | 4 ++-- drushlauncher/drushlauncher_test.go | 8 ++++---- main.go | 9 +++++++-- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/drushlauncher/drushlauncher.go b/drushlauncher/drushlauncher.go index eaf74b2..91dbd53 100644 --- a/drushlauncher/drushlauncher.go +++ b/drushlauncher/drushlauncher.go @@ -6,7 +6,7 @@ import ( "path/filepath" ) -func FindDrupalRoot(path string) (string, error) { +func FindDrushExecutable(path string) (string, error) { // Check if the vendor/bin/drush directory exists in the current directory drushDir := filepath.Join(path, "vendor", "bin", "drush") if _, err := os.Stat(drushDir); err == nil { @@ -29,5 +29,5 @@ func FindDrupalRoot(path string) (string, error) { } // Recursively continue searching in the parent directory - return FindDrupalRoot(parentDir) + return FindDrushExecutable(parentDir) } diff --git a/drushlauncher/drushlauncher_test.go b/drushlauncher/drushlauncher_test.go index 9e0569a..b347f00 100644 --- a/drushlauncher/drushlauncher_test.go +++ b/drushlauncher/drushlauncher_test.go @@ -6,7 +6,7 @@ import ( "testing" ) -func TestFindDrupalRoot(t *testing.T) { +func TestFindDrushExecutable(t *testing.T) { // Set up a temporary directory for the tests tmpDir := os.TempDir() defer os.RemoveAll(tmpDir) @@ -15,7 +15,7 @@ func TestFindDrupalRoot(t *testing.T) { testDir1 := filepath.Join(tmpDir, "testDir1", "vendor", "bin", "drush") os.MkdirAll(filepath.Dir(testDir1), os.ModePerm) os.Create(testDir1) - drupalRoot, err := FindDrupalRoot(testDir1) + drupalRoot, err := FindDrushExecutable(testDir1) if err != nil || drupalRoot != filepath.Join(tmpDir, "testDir1") { t.Errorf("Test case 1 failed: Expected Drupal root %s, got %s, error: %v", filepath.Join(tmpDir, "testDir1"), drupalRoot, err) } @@ -24,14 +24,14 @@ func TestFindDrupalRoot(t *testing.T) { testDir2 := filepath.Join(tmpDir, "testDir2", "vendor", "bin", "drush") os.MkdirAll(filepath.Dir(testDir2), os.ModePerm) os.Create(testDir2) - drupalRoot, err = FindDrupalRoot(testDir2) + drupalRoot, err = FindDrushExecutable(testDir2) if err != nil || drupalRoot != filepath.Join(tmpDir, "testDir2") { t.Errorf("Test case 2 failed: Expected Drupal root %s, got %s, error: %v", filepath.Join(tmpDir, "testDir2"), drupalRoot, err) } // Test case 3: Drupal root not found testDir3 := filepath.Join(tmpDir, "testDir3", "some", "random", "path") - drupalRoot, err = FindDrupalRoot(testDir3) + drupalRoot, err = FindDrushExecutable(testDir3) if err == nil || drupalRoot != "" { t.Errorf("Test case 3 failed: Expected no Drupal root, got %s, error: %v", drupalRoot, err) } diff --git a/main.go b/main.go index 5dd2295..da2b112 100644 --- a/main.go +++ b/main.go @@ -39,7 +39,12 @@ func main() { // Use the alternative Drupal root if provided if altRoot != "" { - drupalRoot = altRoot + var err error + drupalRoot, err = drushlauncher.FindDrushExecutable(altRoot) + if err != nil { + fmt.Println(err) + os.Exit(1) + } } else { // If no alternative root provided, find the Drupal root from the current directory cwd, err := os.Getwd() @@ -47,7 +52,7 @@ func main() { fmt.Println("Error getting current directory:", err) os.Exit(1) } - drupalRoot, err = drushlauncher.FindDrupalRoot(cwd) + drupalRoot, err = drushlauncher.FindDrushExecutable(cwd) if err != nil { fmt.Println(err) os.Exit(1)