-
Notifications
You must be signed in to change notification settings - Fork 557
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Look up git metadata via GitHub env vars when available
and fall back to `git` operations otherwise. This also changes the way that a `git` repo is detected to use `git` itself instead of checking for `.git` directory existence. (linked to internal issue PLAT-228)
- Loading branch information
1 parent
cfa064a
commit 865780a
Showing
2 changed files
with
115 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package image | ||
|
||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func setupGitWorkTree(t *testing.T) string { | ||
r := require.New(t) | ||
|
||
tmp := t.TempDir() | ||
|
||
r.NoError(exec.Command("git", "init", tmp).Run()) | ||
r.NoError(exec.Command("git", "-C", tmp, "commit", "--allow-empty", "-m", "walrus").Run()) | ||
r.NoError(exec.Command("git", "-C", tmp, "tag", "-a", "v0.0.1+walrus", "-m", "walrus time").Run()) | ||
|
||
return tmp | ||
} | ||
|
||
func TestIsGitWorkTree(t *testing.T) { | ||
r := require.New(t) | ||
|
||
r.False(isGitWorkTree("/dev/null")) | ||
r.True(isGitWorkTree(setupGitWorkTree(t))) | ||
} | ||
|
||
func TestGitHead(t *testing.T) { | ||
r := require.New(t) | ||
tmp := setupGitWorkTree(t) | ||
|
||
head, err := gitHead(tmp) | ||
r.NoError(err) | ||
r.NotEqual("", head) | ||
|
||
head, err = gitHead("/dev/null") | ||
r.Error(err) | ||
r.Equal("", head) | ||
|
||
t.Setenv("GITHUB_SHA", "fafafaf") | ||
|
||
head, err = gitHead("/dev/null") | ||
r.NoError(err) | ||
r.Equal("fafafaf", head) | ||
} | ||
|
||
func TestGitTag(t *testing.T) { | ||
r := require.New(t) | ||
tmp := setupGitWorkTree(t) | ||
|
||
tag, err := gitTag(tmp) | ||
r.NoError(err) | ||
r.Equal("v0.0.1+walrus", tag) | ||
|
||
tag, err = gitTag("/dev/null") | ||
r.Error(err) | ||
r.Equal("", tag) | ||
|
||
t.Setenv("GITHUB_REF_NAME", "v0.0.1+manatee") | ||
|
||
tag, err = gitTag("/dev/null") | ||
r.NoError(err) | ||
r.Equal("v0.0.1+manatee", tag) | ||
} |