-
Notifications
You must be signed in to change notification settings - Fork 319
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement support for BAZELISK_FORMAT_URL #427
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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,78 @@ | ||
package core | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/bazelbuild/bazelisk/platforms" | ||
) | ||
|
||
func TestBuildURLFromFormat(t *testing.T) { | ||
osName, err := platforms.DetermineOperatingSystem() | ||
if err != nil { | ||
t.Fatalf("Cannot get operating system name: %v", err) | ||
} | ||
|
||
version := "6.0.0" | ||
|
||
machineName, err := platforms.DetermineArchitecture(osName, version) | ||
if err != nil { | ||
t.Fatalf("Cannot get machine architecture name: %v", err) | ||
} | ||
|
||
suffix := platforms.DetermineExecutableFilenameSuffix() | ||
|
||
previousSha256, hadSha256 := os.LookupEnv("BAZELISK_VERIFY_SHA256") | ||
sha256 := "SomeSha256ValueThatIsIrrelevant" | ||
if err := os.Setenv("BAZELISK_VERIFY_SHA256", sha256); err != nil { | ||
t.Fatalf("Failed to set BAZELISK_VERIFY_SHA256") | ||
} | ||
defer func() { | ||
if hadSha256 { | ||
os.Setenv("BAZELISK_VERIFY_SHA256", previousSha256) | ||
} else { | ||
os.Unsetenv("BAZELISK_VERIFY_SHA256") | ||
} | ||
}() | ||
|
||
type test struct { | ||
format string | ||
want string | ||
wantErr error | ||
} | ||
|
||
tests := []test{ | ||
{format: "", want: ""}, | ||
{format: "no/placeholders", want: "no/placeholders"}, | ||
|
||
{format: "%", wantErr: errors.New("trailing %")}, | ||
{format: "%%", want: "%"}, | ||
{format: "%%%%", want: "%%"}, | ||
{format: "invalid/trailing/%", wantErr: errors.New("trailing %")}, | ||
{format: "escaped%%placeholder", want: "escaped%placeholder"}, | ||
|
||
{format: "foo-%e-bar", want: fmt.Sprintf("foo-%s-bar", suffix)}, | ||
{format: "foo-%h-bar", want: fmt.Sprintf("foo-%s-bar", sha256)}, | ||
{format: "foo-%m-bar", want: fmt.Sprintf("foo-%s-bar", machineName)}, | ||
{format: "foo-%o-bar", want: fmt.Sprintf("foo-%s-bar", osName)}, | ||
{format: "foo-%v-bar", want: fmt.Sprintf("foo-%s-bar", version)}, | ||
|
||
{format: "repeated %v %m %v", want: fmt.Sprintf("repeated %s %s %s", version, machineName, version)}, | ||
|
||
{format: "https://real.example.com/%e/%m/%o/%v#%%20trailing", want: fmt.Sprintf("https://real.example.com/%s/%s/%s/%s#%%20trailing", suffix, machineName, osName, version)}, | ||
} | ||
|
||
for _, tc := range tests { | ||
got, err := BuildURLFromFormat(tc.format, version) | ||
if fmt.Sprintf("%v", err) != fmt.Sprintf("%v", tc.wantErr) { | ||
if got != "" { | ||
t.Errorf("format '%s': got non-empty '%s' on error", tc.format, got) | ||
} | ||
t.Errorf("format '%s': got error %v, want error %v", tc.format, err, tc.wantErr) | ||
} else if got != tc.want { | ||
t.Errorf("format '%s': got %s, want %s", tc.format, got, tc.want) | ||
} | ||
} | ||
} |
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
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I acknowledge that adding this feels very strange here because we have an input
downloader
function that should encapsulate this. However please note that the previous code was already hacking its way to handle the base URL case in the same way, so I just followed existing practice.I think that this could be generalized to inject these behaviors via
downloader
functions... but I'd rather investigate that separately because there are lots of subtle consequences...