-
Notifications
You must be signed in to change notification settings - Fork 3
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
martacarbone
wants to merge
9
commits into
main
Choose a base branch
from
cli_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.
+194
−8
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
fd939c5
Add the server version to arduino-app-cli version.
martacarbone 466245c
Add copyright header.
martacarbone bf435c6
Address review comments.
martacarbone a5d95ea
Fix lint.
martacarbone 776bcca
Address review
martacarbone 7aba0e1
Define the port only as custom input.
martacarbone a9b0ca2
Update cmd/arduino-app-cli/version/version.go
martacarbone ef78764
Update cmd/arduino-app-cli/version/version.go
martacarbone 0f6e212
Update cmd/arduino-app-cli/version/version.go
martacarbone 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
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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 provided host:port.", | ||
| 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, | ||
| Version: clientVersion, | ||
| DaemonVersion: "3.0", | ||
| }, | ||
| hostAndPort: "localhost:8800", | ||
| }, | ||
| { | ||
| name: "return error if default server is not listening", | ||
| serverStub: failureServer, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| Version: clientVersion, | ||
| DaemonVersion: daemonVersion, | ||
| }, | ||
| hostAndPort: unreacheableUrl, | ||
| }, | ||
| { | ||
| name: "return error if provided server is not listening", | ||
| serverStub: failureServer, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| Version: clientVersion, | ||
| DaemonVersion: daemonVersion, | ||
| }, | ||
| hostAndPort: unreacheableUrl, | ||
| }, | ||
| { | ||
| name: "return error for server resopnse 500 Internal Server Error", | ||
| serverStub: failureInternalServerError, | ||
| expectedResult: versionResult{ | ||
| Name: ProgramName, | ||
| Version: 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) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I usually prefer using the httptest module instead of mocking the http client |
||
|
|
||
| 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 | ||
| }) | ||
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.
nit