Skip to content

Commit

Permalink
Fix timewarp pypi support (#353)
Browse files Browse the repository at this point in the history
  • Loading branch information
msuozzo authored Feb 21, 2025
1 parent ceb0b84 commit 082d3f1
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 5 deletions.
4 changes: 2 additions & 2 deletions internal/timewarp/timewarp.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (

var (
npmRegistry = urlx.MustParse("https://registry.npmjs.org/")
pypiRegistry = urlx.MustParse("https://pypi.org/simple")
pypiRegistry = urlx.MustParse("https://pypi.org/")
lowTimeBound = time.Date(2000, time.January, 1, 0, 0, 0, 0, time.UTC)
)

Expand Down Expand Up @@ -295,7 +295,7 @@ func timeWarpPyPIProjectRequest(client httpx.BasicClient, obj map[string]any, at
if t.Before(at.Add(time.Second)) {
pastFiles = append(pastFiles, file)
}
if t.Before(firstSeen) {
if t.Before(firstSeen) || firstSeen.IsZero() {
firstSeen = t
}
}
Expand Down
92 changes: 89 additions & 3 deletions internal/timewarp/timewarp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
},
},
},
URLValidator: func(expected, actual string) {
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("URL mismatch (-want +got):\n%s", diff)
}
},
},
want: &http.Response{
StatusCode: http.StatusOK,
Expand Down Expand Up @@ -89,13 +94,13 @@ func TestHandler_ServeHTTP(t *testing.T) {
},
{
name: "pypi project request - successful time warp",
url: "http://localhost:8081/some-package",
url: "http://localhost:8081/pypi/some-package/json",
basicAuth: "pypi:2022-01-01T00:00:00Z",
client: &httpxtest.MockClient{
Calls: []httpxtest.Call{
{
Method: "GET",
URL: "https://pypi.org/simple/some-package",
URL: "https://pypi.org/pypi/some-package/json",
Response: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
Expand Down Expand Up @@ -126,7 +131,7 @@ func TestHandler_ServeHTTP(t *testing.T) {
},
{
Method: "GET",
URL: "https://pypi.org/simple/pypi/some-package/1.0.0/json",
URL: "https://pypi.org/pypi/some-package/1.0.0/json",
Response: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
Expand All @@ -142,6 +147,11 @@ func TestHandler_ServeHTTP(t *testing.T) {
},
},
},
URLValidator: func(expected, actual string) {
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("URL mismatch (-want +got):\n%s", diff)
}
},
},
want: &http.Response{
StatusCode: http.StatusOK,
Expand All @@ -165,6 +175,82 @@ func TestHandler_ServeHTTP(t *testing.T) {
}`)),
},
},
{
name: "pypi project request - timewarp but no available packages",
url: "http://localhost:8081/pypi/some-package/json",
basicAuth: "pypi:2022-01-01T00:00:00Z",
client: &httpxtest.MockClient{
Calls: []httpxtest.Call{
{
Method: "GET",
URL: "https://pypi.org/pypi/some-package/json",
Response: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(bytes.NewBufferString(`{
"info": {
"name": "some-package",
"version": "2.0.0",
"requires_dist": ["req1", "req2", "req3"]
},
"releases": {
"1.0.0": [
{
"upload_time_iso_8601": "2023-06-01T00:00:00Z",
"filename": "some-package-1.0.0.tar.gz"
}
],
"2.0.0": [
{
"upload_time_iso_8601": "2024-06-01T00:00:00Z",
"filename": "some-package-2.0.0.tar.gz"
}
]
}
}`)),
},
},
{
Method: "GET",
URL: "https://pypi.org/pypi/some-package/json",
Response: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(bytes.NewBufferString(`{
"info": {
"name": "some-package",
"version": "1.0.0",
"requires_dist": ["req1", "req2"]
}
}`)),
},
},
},
URLValidator: func(expected, actual string) {
if diff := cmp.Diff(expected, actual); diff != "" {
t.Fatalf("URL mismatch (-want +got):\n%s", diff)
}
},
},
want: &http.Response{
StatusCode: http.StatusOK,
Header: http.Header{
"Content-Type": []string{"application/json"},
},
Body: io.NopCloser(bytes.NewBufferString(`{
"info": {
"name": "some-package",
"version": "1.0.0",
"requires_dist": ["req1", "req2"]
},
"releases": {}
}`)),
},
},
{
name: "invalid platform",
url: "http://localhost:8081/some-package",
Expand Down

0 comments on commit 082d3f1

Please sign in to comment.