Skip to content

Commit

Permalink
Perform direct asset name comparison (#102)
Browse files Browse the repository at this point in the history
* Perform direct asset name comparison

* Add tests to verify direct asset name comparison

* Update asset version

* Fix TestGetListOfReleasesFromGitHubRepo test

* Fix TestDownloadReleaseAssetsWithRegexCharacters test
  • Loading branch information
tonerdo authored Jan 24, 2022
1 parent 7b83297 commit 625331e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
2 changes: 1 addition & 1 deletion github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestGetListOfReleasesFromGitHubRepo(t *testing.T) {
testInst GitHubInstance
}{
// Test on a public repo whose sole purpose is to be a test fixture for this tool
{"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.3", 3, "", testInst},
{"https://github.com/gruntwork-io/fetch-test-public", "v0.0.1", "v0.0.4", 4, "", testInst},

// Private repo equivalent
{"https://github.com/gruntwork-io/fetch-test-private", "v0.0.2", "v0.0.2", 1, os.Getenv("GITHUB_OAUTH_TOKEN"), testInst},
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,11 @@ func findAssetsInRelease(assetRegex string, release GitHubReleaseApiResponse) ([
if matched {
assetRef := asset
matches = append(matches, &assetRef)
} else if asset.Name == assetRegex {
// Sometimes the actual asset name contains regex symbols that could mess up matching.
// Perform a direct comparison as a last resort.
assetRef := asset
matches = append(matches, &assetRef)
}
}

Expand Down
35 changes: 35 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,41 @@ func TestDownloadReleaseAssets(t *testing.T) {
}
}

func TestDownloadReleaseAssetsWithRegexCharacters(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
testInst := GitHubInstance{
BaseUrl: "github.com",
ApiUrl: "api.github.com",
}

const githubRepoUrl = "https://github.com/gruntwork-io/fetch-test-public"
const releaseAsset = "hello+world.txt"
const assetVersion = "v0.0.4"

githubRepo, err := ParseUrlIntoGitHubRepo(githubRepoUrl, "", testInst)
if err != nil {
t.Fatalf("Failed to parse sample release asset GitHub URL into Fetch GitHubRepo struct: %s", err)
}

assetPaths, fetchErr := downloadReleaseAssets(logger, releaseAsset, tmpDir, githubRepo, assetVersion, false)
if fetchErr != nil {
t.Fatalf("Failed to download release asset: %s", fetchErr)
}

if len(assetPaths) != 1 {
t.Fatalf("Expected to download 1 assets, not %d", len(assetPaths))
}

assetPath := assetPaths[0]

if _, err := os.Stat(assetPath); os.IsNotExist(err) {
t.Fatalf("Downloaded file should exist at %s", assetPath)
} else {
fmt.Printf("Verified the downloaded asset exists at %s\n", assetPath)
}
}

func TestInvalidReleaseAssetsRegex(t *testing.T) {
tmpDir := mkTempDir(t)
logger := GetProjectLogger()
Expand Down

0 comments on commit 625331e

Please sign in to comment.