Skip to content

Commit

Permalink
create fake .git directory to test detecting repository root (#307)
Browse files Browse the repository at this point in the history
instead of using actionlint's `.git` directory
  • Loading branch information
rhysd committed Jun 20, 2023
1 parent 2d9b82a commit 0a5b35d
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 3 deletions.
7 changes: 5 additions & 2 deletions linter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,15 @@ func TestLinterLintError(t *testing.T) {
}

func TestLintFindProjectFromPath(t *testing.T) {
d := filepath.Join("testdata", "projects", "broken_reusable_workflow")
f := filepath.Join(d, "workflows", "test.yaml")
d := filepath.Join("testdata", "find_project")
f := filepath.Join(d, ".github", "workflows", "test.yaml")
b, err := os.ReadFile(f)
if err != nil {
panic(err)
}

testEnsureDotGitDir(d)

lint := func(path string) []*Error {
l, err := NewLinter(io.Discard, &LinterOptions{})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion project.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ func NewProjects() *Projects {
return &Projects{}
}

// At returns the Project instance which the path belongs to.
// At returns the Project instance which the path belongs to. It returns nil if no project is found
// from the path.
func (ps *Projects) At(path string) *Project {
for _, p := range ps.known {
if p.Knows(path) {
Expand Down
90 changes: 90 additions & 0 deletions project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package actionlint

import (
"os"
"path/filepath"
"testing"
)

// Create `.git` directory since actionlint finds the directory to detect the repository root.
// Without creating this directory, this test case will fail when `actionlint/.git` directory
// doesn't exist. When cloning actionlint repository with Git, it never happens. However, when
// downloading sources tarball from github.com, it doesn't contain `.git` directory so it
// happens. Please see #307 for more details.
func testEnsureDotGitDir(dir string) {
d := filepath.Join(dir, ".git")
if err := os.MkdirAll(d, 0750); err != nil {
panic(err)
}
}

func TestProjectsFindProjectFromPath(t *testing.T) {
d := filepath.Join("testdata", "find_project")
abs, err := filepath.Abs(d)
if err != nil {
panic(err)
}
testEnsureDotGitDir(d)

ps := NewProjects()
for _, tc := range []struct {
what string
path string
}{
{
what: "project root",
path: d,
},
{
what: "workflows directory",
path: filepath.Join(d, ".github", "workflows"),
},
{
what: "workflow file",
path: filepath.Join(d, ".github", "workflows", "test.yaml"),
},
{
what: "outside workflows directory",
path: filepath.Join(d, ".github", "reusable", "broken.yaml"),
},
{
what: "directory outside .github",
path: filepath.Join(d, "foo"),
},
{
what: "file outside .github",
path: filepath.Join(d, "foo", "test.txt"),
},
} {
t.Run(tc.what, func(t *testing.T) {
p := ps.At(tc.path)

r := p.RootDir()
if r != abs {
t.Fatalf("root directory of project %v should be %q but got %q", p, abs, r)
}

// Result should be cached
p2 := ps.At(tc.path)
if p != p2 {
t.Fatalf("project %v is not cached. New project is %v. %p v.s. %p", p, p2, p, p2)
}
})
}
}

func TestProjectsDoesNotFindProjectFromOutside(t *testing.T) {
d := filepath.Join("testdata", "find_project")
abs, err := filepath.Abs(d)
if err != nil {
panic(err)
}
testEnsureDotGitDir(d)

outside := filepath.Join(d, "..")
ps := NewProjects()
p := ps.At(outside)
if p != nil && p.RootDir() == abs {
t.Fatalf("project %v is detected from outside of the project %q", p, outside)
}
}
9 changes: 9 additions & 0 deletions testdata/find_project/.github/reusable/broken.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
on:
workflow_call:
outputs: this causes an error

jobs:
test:
runs-on: ubuntu-latest
steps:
- run: echo ...
5 changes: 5 additions & 0 deletions testdata/find_project/.github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
on: push

jobs:
caller:
uses: ./.github/reusable/broken.yaml
6 changes: 6 additions & 0 deletions testdata/find_project/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
This directory is used for testing that actionlint can detect a repository root.

- `TestLintFindProjectFromPath` in `linter_test.go`
- `project_test.go`

`.git` directory is dynamically created when the test case is run because Git doesn't allow committing `.git` directory.

0 comments on commit 0a5b35d

Please sign in to comment.