Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8f6f557

Browse files
authored
Downloader helper user agent (#227)
* add global variables and Info struct in order to inject build time values via ldflags * replace cli.AppName with global variable global.GetAppName() getter * replaced cli.Version var with global getter * add .idea to .gitignore * re organize imports * add license to global.go * add hardcoded fallback version string * organize imports step2 * organize imports step3 * replace cli.AppName with global variable global.GetAppName() getter * add User-Agent header to downloader helper configuration * add testing for user agent-string generation * re organize imports * refactor global package into version package and rename and replace having cli login,logout,validate removed * refactor versioning variables in version package and implement Info struct wihth String method * refactor Info cosntructor to use directly package vars and renamed package from global to version * replace package name from global to version * solve package name clash for version and cli/version * replace application name getter with VersionInfo.Application field having removed cli login,logout,validate * search and replace version string getter with VersionInfo.VersionString field * implement empty http.Header struct propagation from cli to resources * align downloader helper to use propagated http.Header from upper layers and modify test accordingly * align daemon to use empty http.Header as struct field in ArduinoCoreServerImpl * clean inside cobra commands for empty http.Header struct and inject headers build from VersionInfo struct in cli InitInstance func * remove unused runDaemonCommand in daemon * add user agent specific for daemon mode via DownloaderHeaders property for ArduinoCoreServerImpl * add testing information in README.md * removed getters and related usage for version package * update dependencies * finalize helpers_test User-Agent header value * add integration test for version info injection via vars * tidy go mod and reorganize imports and tidy long lines * inject ldflags variables in build task in Taskfile.yml * replace commands in /travis.yml with tasks properly merging flags * fix install command for go-task to solve "undefined: interp.EnvFromList" error * add coverage files to .gitignore * tidy dependencies in go.mod * update task executable path in tasks due to install path generated by go-task install script * implement exported variable cli.HTTPClientHeader in order to have it available in all cli subpackages and not onli in cli * fix comment replacing "version config" with "global config" * leverage cli.VersionInfo to present a better terminal and json data for version info
1 parent fe92910 commit 8f6f557

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+419
-160
lines changed

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55
/cmd/formatter/debug.test
66
/arduino-cli.yaml
77
/wiki
8+
.idea
9+
coverage_*.txt

‎.travis.yml‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ env:
1111
# Make sure golangci-lint is vendored.
1212
before_install:
1313
- curl -sfL https://install.goreleaser.com/github.com/golangci/golangci-lint.sh | bash -s -- -b $GOPATH/bin v1.16.0
14+
- curl -sL https://taskfile.dev/install.sh | sh
1415

1516
install: true
1617

@@ -20,9 +21,10 @@ script:
2021
# Run linter
2122
- golangci-lint run
2223
# Build and test
23-
- go build
24-
- go test -timeout 20m -v -coverpkg=./... -coverprofile=coverage.txt -covermode=atomic ./...
24+
- ./bin/task build
25+
- ./bin/task test
2526

2627
after_success:
27-
- bash <(curl -s https://codecov.io/bash)
28+
- bash <(curl -s https://codecov.io/bash) -cF unittests,integration
29+
2830

‎README.md‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,3 +333,20 @@ Because:
333333
#### How can I find the core/FQBN for a board?
334334

335335
See: https://github.com/arduino/arduino-cli#step-4-find-and-install-the-right-core
336+
337+
# Testing
338+
339+
Currently Unit and Integration test are available for launch in 2 ways:
340+
341+
1. classic `go test ./...` to launch both unit and integration test
342+
343+
2. via [task](https://taskfile.dev) that includes the following options:
344+
345+
```
346+
* build: Build the project
347+
* test: Run the full testsuite
348+
* test-integration: Run integration tests only
349+
* test-unit: Run unit tests only
350+
```
351+
352+
For Example to launch unit tests only run: `task test-unit`

‎Taskfile.yml‎

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ tasks:
44
build:
55
desc: Build the project
66
cmds:
7-
- go build -v -i
7+
- go build -v -i {{.LDFLAGS}}
88

99
test:
1010
desc: Run the full testsuite
@@ -15,9 +15,26 @@ tasks:
1515
test-unit:
1616
desc: Run unit tests only
1717
cmds:
18-
- go test -short {{ default "-v" .GOFLAGS }} {{ default "./..." .TARGETS }}
18+
- go test -short {{ default "-v" .GOFLAGS }} -coverprofile=coverage_unit.txt {{ default "./..." .TARGETS }}
1919

2020
test-integration:
2121
desc: Run integration tests only
2222
cmds:
23-
- go test -run Integration {{ default "-v" .GOFLAGS }} {{ default "./..." .TARGETS }}
23+
- go test -run Integration {{ default "-v" .GOFLAGS }} -coverprofile=coverage_integ.txt {{ default "./..." .TARGETS }} {{.TEST_LDFLAGS}}
24+
25+
vars:
26+
# build vars
27+
VERSIONSTRING: "0.3.6-alpha.preview"
28+
COMMIT:
29+
sh: echo ${TRAVIS_COMMIT:-`git log -n 1 --format=%h`}
30+
LDFLAGS: >
31+
-ldflags '-X github.com/arduino/arduino-cli/version.versionString={{.VERSIONSTRING}}
32+
-X github.com/arduino/arduino-cli/version.commit={{.COMMIT}}'
33+
34+
# test vars
35+
GOFLAGS: "-timeout 5m -v -coverpkg=./... -covermode=atomic"
36+
TEST_VERSIONSTRING: "0.0.0-test.preview"
37+
TEST_COMMIT: "deadbeef"
38+
TEST_LDFLAGS: >
39+
-ldflags '-X github.com/arduino/arduino-cli/version.versionString={{.TEST_VERSIONSTRING}}
40+
-X github.com/arduino/arduino-cli/version.commit={{.TEST_COMMIT}}'

‎arduino/cores/packagemanager/download.go‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package packagemanager
1919

2020
import (
2121
"fmt"
22+
"net/http"
2223

2324
"github.com/arduino/arduino-cli/arduino/cores"
2425
"go.bug.st/downloader"
@@ -102,16 +103,16 @@ func (pm *PackageManager) FindPlatformReleaseDependencies(item *PlatformReferenc
102103

103104
// DownloadToolRelease downloads a ToolRelease. If the tool is already downloaded a nil Downloader
104105
// is returned.
105-
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease) (*downloader.Downloader, error) {
106+
func (pm *PackageManager) DownloadToolRelease(tool *cores.ToolRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
106107
resource := tool.GetCompatibleFlavour()
107108
if resource == nil {
108109
return nil, fmt.Errorf("tool not available for your OS")
109110
}
110-
return resource.Download(pm.DownloadDir)
111+
return resource.Download(pm.DownloadDir, downloaderHeaders)
111112
}
112113

113114
// DownloadPlatformRelease downloads a PlatformRelease. If the platform is already downloaded a
114115
// nil Downloader is returned.
115-
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease) (*downloader.Downloader, error) {
116-
return platform.Resource.Download(pm.DownloadDir)
116+
func (pm *PackageManager) DownloadPlatformRelease(platform *cores.PlatformRelease, downloaderHeaders http.Header) (*downloader.Downloader, error) {
117+
return platform.Resource.Download(pm.DownloadDir, downloaderHeaders)
117118
}

‎arduino/resources/helpers.go‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,10 @@ package resources
1919

2020
import (
2121
"fmt"
22+
"net/http"
2223
"os"
2324

24-
paths "github.com/arduino/go-paths-helper"
25+
"github.com/arduino/go-paths-helper"
2526
"go.bug.st/downloader"
2627
)
2728

@@ -45,7 +46,7 @@ func (r *DownloadResource) IsCached(downloadDir *paths.Path) (bool, error) {
4546
}
4647

4748
// Download a DownloadResource.
48-
func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downloader, error) {
49+
func (r *DownloadResource) Download(downloadDir *paths.Path, downloaderHeaders http.Header) (*downloader.Downloader, error) {
4950
cached, err := r.TestLocalArchiveIntegrity(downloadDir)
5051
if err != nil {
5152
return nil, fmt.Errorf("testing local archive integrity: %s", err)
@@ -73,5 +74,7 @@ func (r *DownloadResource) Download(downloadDir *paths.Path) (*downloader.Downlo
7374
return nil, fmt.Errorf("getting archive file info: %s", err)
7475
}
7576

76-
return downloader.Download(path.String(), r.URL)
77+
downloadConfig := downloader.Config{
78+
RequestHeaders: downloaderHeaders}
79+
return downloader.DownloadWithConfig(path.String(), r.URL, downloadConfig)
7780
}

‎arduino/resources/helpers_test.go‎

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* This file is part of arduino-cli.
3+
*
4+
* Copyright 2018 ARDUINO SA (http://www.arduino.cc/)
5+
*
6+
* This software is released under the GNU General Public License version 3,
7+
* which covers the main part of arduino-cli.
8+
* The terms of this license can be found at:
9+
* https://www.gnu.org/licenses/gpl-3.0.en.html
10+
*
11+
* You can be released from the requirements of the above licenses by purchasing
12+
* a commercial license. Buying such a license is mandatory if you want to modify or
13+
* otherwise use the software for commercial activities involving the Arduino
14+
* software without disclosing the source code of your own applications. To purchase
15+
* a commercial license, send an email to license@arduino.cc.
16+
*/
17+
18+
package resources
19+
20+
import (
21+
"fmt"
22+
"io/ioutil"
23+
"net/http"
24+
"net/http/httptest"
25+
"strings"
26+
"testing"
27+
28+
"github.com/arduino/go-paths-helper"
29+
"github.com/stretchr/testify/require"
30+
)
31+
32+
type EchoHandler struct{}
33+
34+
// EchoHandler echos back the request as a response if used as http handler
35+
func (h *EchoHandler) ServeHTTP(writer http.ResponseWriter, request *http.Request) {
36+
request.Write(writer)
37+
}
38+
39+
func TestDownloadApplyUserAgentHeaderUsingConfig(t *testing.T) {
40+
goldUserAgentValue := fmt.Sprintf("arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019年06月12日 11:11:11.111")
41+
goldUserAgentString := "User-Agent: " + goldUserAgentValue
42+
43+
tmp, err := paths.MkTempDir("", "")
44+
require.NoError(t, err)
45+
defer tmp.RemoveAll()
46+
47+
// startup echo server
48+
srv := httptest.NewServer(&EchoHandler{})
49+
defer srv.Close()
50+
51+
r := &DownloadResource{
52+
ArchiveFileName: "echo.txt",
53+
CachePath: "cache",
54+
URL: srv.URL,
55+
}
56+
57+
d, err := r.Download(tmp, http.Header{"User-Agent": []string{goldUserAgentValue}})
58+
require.NoError(t, err)
59+
err = d.Run()
60+
require.NoError(t, err)
61+
62+
// leverage the download helper to download the echo for the request made by the downloader itself
63+
//
64+
// expect something like:
65+
// GET /echo HTTP/1.1
66+
// Host: 127.0.0.1:64999
67+
// User-Agent: arduino-cli/0.0.0-test.preview (amd64; linux; go1.12.4) Commit:deadbeef/Build:2019年06月12日 11:11:11.111
68+
// Accept-Encoding: gzip
69+
70+
b, err := ioutil.ReadFile(tmp.String() + "/cache/echo.txt") // just pass the file name
71+
require.NoError(t, err)
72+
73+
requestLines := strings.Split(string(b), "\r\n")
74+
userAgentHeaderString := ""
75+
for _, line := range requestLines {
76+
if strings.Contains(line, "User-Agent: ") {
77+
userAgentHeaderString = line
78+
}
79+
}
80+
require.Equal(t, goldUserAgentString, userAgentHeaderString)
81+
82+
}

‎arduino/resources/resources_test.go‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ package resources
2020
import (
2121
"crypto"
2222
"encoding/hex"
23+
"net/http"
2324
"testing"
2425

2526
paths "github.com/arduino/go-paths-helper"
@@ -43,7 +44,7 @@ func TestDownloadAndChecksums(t *testing.T) {
4344
require.NoError(t, err)
4445

4546
downloadAndTestChecksum := func() {
46-
d, err := r.Download(tmp)
47+
d, err := r.Download(tmp, http.Header{})
4748
require.NoError(t, err)
4849
err = d.Run()
4950
require.NoError(t, err)
@@ -59,7 +60,7 @@ func TestDownloadAndChecksums(t *testing.T) {
5960
downloadAndTestChecksum()
6061

6162
// Download with cached file
62-
d, err := r.Download(tmp)
63+
d, err := r.Download(tmp, http.Header{})
6364
require.NoError(t, err)
6465
require.Nil(t, d)
6566

‎cli/board/attach.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ func initAttachCommand() *cobra.Command {
3333
Use: "attach <port>|<FQBN> [sketchPath]",
3434
Short: "Attaches a sketch to a board.",
3535
Long: "Attaches a sketch to a board.",
36-
Example: " " + cli.AppName + " board attach serial:///dev/tty/ACM0\n" +
37-
" " + cli.AppName + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
38-
" " + cli.AppName + " board attach arduino:samd:mkr1000",
36+
Example: " " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0\n" +
37+
" " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0 HelloWorld\n" +
38+
" " + cli.VersionInfo.Application + " board attach arduino:samd:mkr1000",
3939
Args: cobra.RangeArgs(1, 2),
4040
Run: runAttachCommand,
4141
}

‎cli/board/board.go‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ func InitCommand() *cobra.Command {
2929
Short: "Arduino board commands.",
3030
Long: "Arduino board commands.",
3131
Example: " # Lists all connected boards.\n" +
32-
" " + cli.AppName + " board list\n\n" +
32+
" " + cli.VersionInfo.Application + " board list\n\n" +
3333
" # Attaches a sketch to a board.\n" +
34-
" " + cli.AppName + " board attach serial:///dev/tty/ACM0 mySketch",
34+
" " + cli.VersionInfo.Application + " board attach serial:///dev/tty/ACM0 mySketch",
3535
}
3636
boardCommand.AddCommand(initAttachCommand())
3737
boardCommand.AddCommand(initDetailsCommand())

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /