Skip to content

Commit 625331e

Browse files
authored
Perform direct asset name comparison (#102)
* Perform direct asset name comparison * Add tests to verify direct asset name comparison * Update asset version * Fix TestGetListOfReleasesFromGitHubRepo test * Fix TestDownloadReleaseAssetsWithRegexCharacters test
1 parent 7b83297 commit 625331e

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

github_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestGetListOfReleasesFromGitHubRepo(t *testing.T) {
2525
testInst GitHubInstance
2626
}{
2727
// Test on a public repo whose sole purpose is to be a test fixture for this tool
28-
{"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.3", 3, "", testInst},
28+
{"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.4", 4, "", testInst},
2929

3030
// Private repo equivalent
3131
{"https://github.com/gruntwork-io/fetch-test-private", "v0.0.2", "v0.0.2", 1, os.Getenv("GITHUB_OAUTH_TOKEN"), testInst},

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,11 @@ func findAssetsInRelease(assetRegex string, release GitHubReleaseApiResponse) ([
455455
if matched {
456456
assetRef := asset
457457
matches = append(matches, &assetRef)
458+
} else if asset.Name == assetRegex {
459+
// Sometimes the actual asset name contains regex symbols that could mess up matching.
460+
// Perform a direct comparison as a last resort.
461+
assetRef := asset
462+
matches = append(matches, &assetRef)
458463
}
459464
}
460465

main_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,41 @@ func TestDownloadReleaseAssets(t *testing.T) {
4242
}
4343
}
4444

45+
func TestDownloadReleaseAssetsWithRegexCharacters(t *testing.T) {
46+
tmpDir := mkTempDir(t)
47+
logger := GetProjectLogger()
48+
testInst := GitHubInstance{
49+
BaseUrl: "github.com",
50+
ApiUrl: "api.github.com",
51+
}
52+
53+
const githubRepoUrl = "https://github.com/gruntwork-io/fetch-test-public"
54+
const releaseAsset = "hello+world.txt"
55+
const assetVersion = "v0.0.4"
56+
57+
githubRepo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, "", testInst)
58+
if err != nil {
59+
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
60+
}
61+
62+
assetPaths, fetchErr := downloadReleaseAssets(logger, releaseAsset, tmpDir, githubRepo, assetVersion, false)
63+
if fetchErr != nil {
64+
t.Fatalf("Failed to download release asset: %s", fetchErr)
65+
}
66+
67+
if len(assetPaths) != 1 {
68+
t.Fatalf("Expected to download 1 assets, not %d", len(assetPaths))
69+
}
70+
71+
assetPath := assetPaths[0]
72+
73+
if _, err := os.Stat(assetPath); os.IsNotExist(err) {
74+
t.Fatalf("Downloaded file should exist at %s", assetPath)
75+
} else {
76+
fmt.Printf("Verified the downloaded asset exists at %s\n", assetPath)
77+
}
78+
}
79+
4580
func TestInvalidReleaseAssetsRegex(t *testing.T) {
4681
tmpDir := mkTempDir(t)
4782
logger := GetProjectLogger()

0 commit comments

Comments
 (0)