Skip to content

Commit 6021de4

Browse files
authored
[search] Fix bad error when installing "rust" (#2284)
## Summary nix search would return `{}` which and we would not return an error. Now we return error. ## How was it tested? `devbox add rust`
1 parent c314509 commit 6021de4

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

internal/nix/search.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,11 @@ func searchSystem(url, system string) (map[string]*Info, error) {
109109
// return ErrPackageNotFound only for that case.
110110
return nil, fmt.Errorf("error searching for pkg %s: %w", url, err)
111111
}
112-
return parseSearchResults(out), nil
112+
parsed := parseSearchResults(out)
113+
if len(parsed) == 0 {
114+
return nil, fmt.Errorf("package not found: %s", url)
115+
}
116+
return parsed, nil
113117
}
114118

115119
// allowableQuery specifies the regex that queries for SearchNixpkgsAttribute must match.

internal/nix/search_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package nix
22

33
import (
4+
"reflect"
45
"testing"
56
)
67

@@ -60,3 +61,52 @@ func TestAllowableQuery(t *testing.T) {
6061
})
6162
}
6263
}
64+
65+
func TestParseSearchResults(t *testing.T) {
66+
testCases := []struct {
67+
name string
68+
input []byte
69+
expectedResult map[string]*Info
70+
}{
71+
{
72+
name: "Valid JSON input",
73+
input: []byte(`{
74+
"go": {
75+
"pname": "go",
76+
"version": "1.20.4"
77+
},
78+
"python3": {
79+
"pname": "python3",
80+
"version": "3.9.16"
81+
}
82+
}`),
83+
expectedResult: map[string]*Info{
84+
"go": {
85+
AttributeKey: "go",
86+
PName: "go",
87+
Version: "1.20.4",
88+
},
89+
"python3": {
90+
AttributeKey: "python3",
91+
PName: "python3",
92+
Version: "3.9.16",
93+
},
94+
},
95+
},
96+
{
97+
name: "Empty JSON input",
98+
input: []byte(`{}`),
99+
expectedResult: map[string]*Info{},
100+
},
101+
}
102+
103+
for _, tc := range testCases {
104+
t.Run(tc.name, func(t *testing.T) {
105+
result := parseSearchResults(tc.input)
106+
107+
if !reflect.DeepEqual(result, tc.expectedResult) {
108+
t.Errorf("Expected result %v, got %v", tc.expectedResult, result)
109+
}
110+
})
111+
}
112+
}

0 commit comments

Comments
 (0)