@@ -164,3 +164,92 @@ func TestParseFlakeRef(t *testing.T) {
164
164
})
165
165
}
166
166
}
167
+
168
+ func TestParseFlakeInstallable (t * testing.T ) {
169
+ cases := map [string ]FlakeInstallable {
170
+ // Empty string is not a valid installable.
171
+ "" : {},
172
+
173
+ // Not a path and not a valid URL.
174
+ "://bad/url" : {},
175
+
176
+ "." : {Ref : FlakeRef {Type : "path" , Path : "." }},
177
+ ".#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "path" , Path : "." }},
178
+ ".#app^out" : {AttrPath : "app" , Outputs : []string {"out" }, Ref : FlakeRef {Type : "path" , Path : "." }},
179
+ ".#app^out,lib" : {AttrPath : "app" , Outputs : []string {"out" , "lib" }, Ref : FlakeRef {Type : "path" , Path : "." }},
180
+ ".#app^*" : {AttrPath : "app" , Outputs : []string {"*" }, Ref : FlakeRef {Type : "path" , Path : "." }},
181
+ ".^*" : {Outputs : []string {"*" }, Ref : FlakeRef {Type : "path" , Path : "." }},
182
+
183
+ "./flake" : {Ref : FlakeRef {Type : "path" , Path : "./flake" }},
184
+ "./flake#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "path" , Path : "./flake" }},
185
+ "./flake#app^out" : {AttrPath : "app" , Outputs : []string {"out" }, Ref : FlakeRef {Type : "path" , Path : "./flake" }},
186
+ "./flake#app^out,lib" : {AttrPath : "app" , Outputs : []string {"out" , "lib" }, Ref : FlakeRef {Type : "path" , Path : "./flake" }},
187
+ "./flake^out" : {Outputs : []string {"out" }, Ref : FlakeRef {Type : "path" , Path : "./flake" }},
188
+
189
+ "indirect" : {Ref : FlakeRef {Type : "indirect" , ID : "indirect" }},
190
+ "nixpkgs#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "indirect" , ID : "nixpkgs" }},
191
+ "nixpkgs#app^out" : {AttrPath : "app" , Outputs : []string {"out" }, Ref : FlakeRef {Type : "indirect" , ID : "nixpkgs" }},
192
+ "nixpkgs#app^out,lib" : {AttrPath : "app" , Outputs : []string {"out" , "lib" }, Ref : FlakeRef {Type : "indirect" , ID : "nixpkgs" }},
193
+ "nixpkgs^out" : {Outputs : []string {"out" }, Ref : FlakeRef {Type : "indirect" , ID : "nixpkgs" }},
194
+
195
+ "%23#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "indirect" , ID : "#" }},
196
+ "./%23#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "path" , Path : "./#" }},
197
+ "/%23#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "path" , Path : "/#" }},
198
+ "path:/%23#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "path" , Path : "/#" }},
199
+ "http://example.com/%23.tar#app" : {AttrPath : "app" , Ref : FlakeRef {Type : "tarball" , URL : "http://example.com/%23.tar#app" }},
200
+ }
201
+
202
+ for installable , want := range cases {
203
+ t .Run (installable , func (t * testing.T ) {
204
+ got , err := ParseFlakeInstallable (installable )
205
+ if diff := cmp .Diff (want , got , cmpopts .IgnoreUnexported (FlakeRef {}, FlakeInstallable {})); diff != "" {
206
+ if err != nil {
207
+ t .Errorf ("got error: %s" , err )
208
+ }
209
+ t .Errorf ("wrong installable (-want +got):\n %s" , diff )
210
+ }
211
+ if err != nil {
212
+ return
213
+ }
214
+ if installable != got .String () {
215
+ t .Errorf ("got.String() = %q != %q" , got , installable )
216
+ }
217
+ })
218
+ }
219
+ }
220
+
221
+ func TestFlakeInstallableDefaultOutputs (t * testing.T ) {
222
+ install := FlakeInstallable {Outputs : nil }
223
+ if ! install .DefaultOutputs () {
224
+ t .Errorf ("DefaultOutputs() = false for nil outputs slice, want true" )
225
+ }
226
+
227
+ install = FlakeInstallable {Outputs : []string {}}
228
+ if ! install .DefaultOutputs () {
229
+ t .Errorf ("DefaultOutputs() = false for empty outputs slice, want true" )
230
+ }
231
+
232
+ install = FlakeInstallable {Outputs : []string {"out" }}
233
+ if install .DefaultOutputs () {
234
+ t .Errorf ("DefaultOutputs() = true for %v, want false" , install .Outputs )
235
+ }
236
+ }
237
+
238
+ func TestFlakeInstallableAllOutputs (t * testing.T ) {
239
+ install := FlakeInstallable {Outputs : []string {"*" }}
240
+ if ! install .AllOutputs () {
241
+ t .Errorf ("AllOutputs() = false for %v, want true" , install .Outputs )
242
+ }
243
+ install = FlakeInstallable {Outputs : []string {"out" , "*" }}
244
+ if ! install .AllOutputs () {
245
+ t .Errorf ("AllOutputs() = false for %v, want true" , install .Outputs )
246
+ }
247
+ install = FlakeInstallable {Outputs : nil }
248
+ if install .AllOutputs () {
249
+ t .Errorf ("AllOutputs() = true for nil outputs slice, want false" )
250
+ }
251
+ install = FlakeInstallable {Outputs : []string {}}
252
+ if install .AllOutputs () {
253
+ t .Errorf ("AllOutputs() = true for empty outputs slice, want false" )
254
+ }
255
+ }
0 commit comments