Skip to content

Commit c2e8fb1

Browse files
Migrate TestUpdateWithUrlNotFound from test_update.py to update_test.go
1 parent d823f9a commit c2e8fb1

File tree

2 files changed

+44
-13
lines changed

2 files changed

+44
-13
lines changed

internal/integrationtest/update/update_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
package update_test
1717

1818
import (
19+
"fmt"
20+
"net/http"
21+
"net/url"
1922
"strings"
2023
"testing"
2124

@@ -68,3 +71,44 @@ func TestUpdateShowingOutdated(t *testing.T) {
6871
require.True(t, strings.HasPrefix(lines[3], "Arduino AVR Boards"))
6972
require.True(t, strings.HasPrefix(lines[6], "USBHost"))
7073
}
74+
75+
func serveFileWithCode(port uint16, path string, code int, env *integrationtest.Environment) *url.URL {
76+
mux := http.NewServeMux()
77+
mux.HandleFunc("/"+path, func(w http.ResponseWriter, r *http.Request) {
78+
w.WriteHeader(code)
79+
})
80+
server := &http.Server{
81+
Addr: fmt.Sprintf(":%d", port),
82+
Handler: mux,
83+
}
84+
85+
t := env.T()
86+
fileURL, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d/%s", port, path))
87+
require.NoError(t, err)
88+
89+
go func() {
90+
err := server.ListenAndServe()
91+
require.Equal(t, err, http.ErrServerClosed)
92+
}()
93+
94+
env.RegisterCleanUpCallback(func() {
95+
server.Close()
96+
})
97+
98+
return fileURL
99+
}
100+
101+
func TestUpdateWithUrlNotFound(t *testing.T) {
102+
env, cli := integrationtest.CreateArduinoCLIWithEnvironment(t)
103+
defer env.CleanUp()
104+
105+
_, _, err := cli.Run("update")
106+
require.NoError(t, err)
107+
108+
// Brings up a local server to fake a failure
109+
url := serveFileWithCode(8080, "test_index.json", 404, env)
110+
111+
stdout, _, err := cli.Run("update", "--additional-urls="+url.String())
112+
require.Error(t, err)
113+
require.Contains(t, string(stdout), "Downloading index: test_index.json Server responded with: 404 Not Found")
114+
}

test/test_update.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,6 @@
1616
from pathlib import Path
1717

1818

19-
def test_update_with_url_not_found(run_command, httpserver):
20-
assert run_command(["update"])
21-
22-
# Brings up a local server to fake a failure
23-
httpserver.expect_request("/test_index.json").respond_with_data(status=404)
24-
url = httpserver.url_for("/test_index.json")
25-
26-
res = run_command(["update", f"--additional-urls={url}"])
27-
assert res.failed
28-
lines = [l.strip() for l in res.stdout.splitlines()]
29-
assert "Downloading index: test_index.json Server responded with: 404 NOT FOUND" in lines
30-
31-
3219
def test_update_with_url_internal_server_error(run_command, httpserver):
3320
assert run_command(["update"])
3421

0 commit comments

Comments
 (0)