Skip to content

Commit

Permalink
Merge pull request #19 from dasginganinja/16-root-with-docroot-path-d…
Browse files Browse the repository at this point in the history
…oesnt-work

#16: Fix alt root lookup by using lookup function. Also refactors
  • Loading branch information
dasginganinja authored Mar 11, 2024
2 parents b171527 + b96a959 commit 79dc9b4
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 8 deletions.
4 changes: 2 additions & 2 deletions drushlauncher/drushlauncher.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -29,5 +29,5 @@ func FindDrupalRoot(path string) (string, error) {
}

// Recursively continue searching in the parent directory
return FindDrupalRoot(parentDir)
return FindDrushExecutable(parentDir)
}
8 changes: 4 additions & 4 deletions drushlauncher/drushlauncher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down
9 changes: 7 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,20 @@ 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()
if err != nil {
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)
Expand Down

0 comments on commit 79dc9b4

Please sign in to comment.