- Notifications
You must be signed in to change notification settings - Fork3
Improve the arduino-app-cli version command by adding the "server version" #31#49
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
martacarbone wants to merge3 commits intomainChoose a base branch fromcli_server_version
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
115 changes: 106 additions & 9 deletionscmd/arduino-app-cli/version/version.go
This file contains hidden or 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
166 changes: 166 additions & 0 deletionscmd/arduino-app-cli/version/version_test.go
This file contains hidden or 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,166 @@ | ||
| // This file is part of arduino-app-cli. | ||
| // | ||
| // Copyright 2025 ARDUINO SA (http://www.arduino.cc/) | ||
| // | ||
| // This software is released under the GNU General Public License version 3, | ||
| // which covers the main part of arduino-app-cli. | ||
| // The terms of this license can be found at: | ||
| // https://www.gnu.org/licenses/gpl-3.0.en.html | ||
| // | ||
| // You can be released from the requirements of the above licenses by purchasing | ||
| // a commercial license. Buying such a license is mandatory if you want to | ||
| // modify or otherwise use the software for commercial activities involving the | ||
| // Arduino software without disclosing the source code of your own applications. | ||
| // To purchase a commercial license, send an email to license@arduino.cc. | ||
| package version | ||
| import ( | ||
| "errors" | ||
| "io" | ||
| "net/http" | ||
| "strings" | ||
| "testing" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
| func TestGetValidUrl(t *testing.T) { | ||
| testCases := []struct { | ||
| name string | ||
| hostPort string | ||
| expectedResult string | ||
| }{ | ||
| { | ||
| name: "Valid host and port should return default.", | ||
| hostPort: "localhost:8800", | ||
| expectedResult: "localhost:8800", | ||
| }, | ||
| { | ||
| name: "Missing host should return default host.", | ||
| hostPort: ":8800", | ||
| expectedResult: "localhost:8800", | ||
| }, | ||
| { | ||
| name: "Missing port should return default port.", | ||
| hostPort: "localhost:", | ||
| expectedResult: "localhost:8800", | ||
| }, | ||
| { | ||
| name: "Custom host and port should return the default.", | ||
| hostPort: "192.168.100.1:1234", | ||
| expectedResult: "192.168.100.1:1234", | ||
| }, | ||
| { | ||
| name: "Host only should return provided input and default port.", | ||
| hostPort: "192.168.1.1", | ||
| expectedResult: "192.168.1.1:8800", | ||
| }, | ||
| { | ||
| name: "Missing host and port should return default.", | ||
| hostPort: "", | ||
| expectedResult: "localhost:8800", | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| url, _ := validateHost(tc.hostPort) | ||
| require.Equal(t, tc.expectedResult, url) | ||
| }) | ||
| } | ||
| } | ||
| func TestServerVersion(t *testing.T) { | ||
| clientVersion := "5.1-dev" | ||
| unreacheableUrl := "unreacheable:123" | ||
| daemonVersion := "" | ||
| testCases := []struct { | ||
| name string | ||
| serverStub Tripper | ||
| expectedResult versionResult | ||
| hostAndPort string | ||
| }{ | ||
| { | ||
| name: "return the server version when the server is up", | ||
| serverStub: successServer, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| ClientVersion: clientVersion, | ||
| DaemonVersion: "3.0", | ||
| }, | ||
| hostAndPort: "localhost:8800", | ||
| }, | ||
| { | ||
| name: "return error if default server is not listening", | ||
| serverStub: failureServer, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| ClientVersion: clientVersion, | ||
| DaemonVersion: daemonVersion, | ||
| }, | ||
| hostAndPort: unreacheableUrl, | ||
| }, | ||
| { | ||
| name: "return error if provided server is not listening", | ||
| serverStub: failureServer, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| ClientVersion: clientVersion, | ||
| DaemonVersion: daemonVersion, | ||
| }, | ||
| hostAndPort: unreacheableUrl, | ||
| }, | ||
| { | ||
| name: "return error for server resopnse 500 Internal Server Error", | ||
| serverStub: failureInternalServerError, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| ClientVersion: clientVersion, | ||
| DaemonVersion: daemonVersion, | ||
| }, | ||
| hostAndPort: unreacheableUrl, | ||
| }, | ||
| } | ||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| // arrange | ||
| httpClient := http.Client{} | ||
| httpClient.Transport = tc.serverStub | ||
| // act | ||
| result, _ := versionHandler(httpClient, clientVersion, tc.hostAndPort) | ||
| // assert | ||
| require.Equal(t, tc.expectedResult, result) | ||
| }) | ||
| } | ||
| } | ||
| // Leverage the http.Client's RoundTripper | ||
| // to return a canned response and bypass network calls. | ||
| type Tripper func(*http.Request) (*http.Response, error) | ||
| func (t Tripper) RoundTrip(request *http.Request) (*http.Response, error) { | ||
| return t(request) | ||
| } | ||
| var successServer = Tripper(func(*http.Request) (*http.Response, error) { | ||
| body := io.NopCloser(strings.NewReader(`{"version":"3.0"}`)) | ||
| return &http.Response{ | ||
| StatusCode: http.StatusOK, | ||
| Body: body, | ||
| }, nil | ||
| }) | ||
| var failureServer = Tripper(func(*http.Request) (*http.Response, error) { | ||
| return nil, errors.New("connetion refused") | ||
| }) | ||
| var failureInternalServerError = Tripper(func(*http.Request) (*http.Response, error) { | ||
| return &http.Response{ | ||
| StatusCode: http.StatusInternalServerError, | ||
| Body: io.NopCloser(strings.NewReader("")), | ||
| }, nil | ||
| }) |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
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.