1- _ : {
1+ {
22 perSystem =
33 { pkgs , self' , ... } :
44 {
4848 self' . packages . flake-edit
4949 ] ;
5050
51+ environment . variables = {
52+ FE_LOG = "debug,hyper=off,h2=off" ;
53+ CI = "1" ;
54+ } ;
55+
5156 nix . settings = {
5257 experimental-features = [
5358 "nix-command"
6368 forgejoExe = "${ nodes . forge . config . services . forgejo . package } /bin/forgejo" ;
6469 in
6570 ''
71+ EMPTY_FLAKE = "{ inputs = { }; outputs = { ... }: { }; }"
72+
73+ def write_empty_flake(machine, path):
74+ machine.succeed(f"mkdir -p $(dirname {path}) && echo '{EMPTY_FLAKE}' > {path}")
75+
6676 start_all()
6777
6878 forge.wait_for_unit("forgejo.service")
98108 """)
99109
100110 with subtest("Create v1.0.0 release"):
101- # Create v1.0.0
102111 forge.succeed(
103112 "curl --fail -X POST http://localhost:3000/api/v1/repos/test/project1/releases "
104113 + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
@@ -129,20 +138,8 @@ _: {
129138 assert "v1.0.0" in releases, "v1.0.0 should be available"
130139
131140 with subtest("Create empty test flake.nix on client"):
132- client.succeed(r"""
133- mkdir -p /tmp/test-flake
134- cat > /tmp/test-flake/flake.nix << 'EOF'
135- {
136- description = "Test flake for flake-edit integration";
137-
138- inputs = {
139- };
140-
141- outputs = { ... }: { };
142- }
143- EOF
144- cat /tmp/test-flake/flake.nix
145- """)
141+ write_empty_flake(client, "/tmp/test-flake/flake.nix")
142+ client.succeed("cat /tmp/test-flake/flake.nix")
146143
147144 with subtest("Verify empty flake has no inputs"):
148145 output = client.succeed("""
@@ -155,40 +152,32 @@ _: {
155152 with subtest("Add input without version pin"):
156153 output = client.succeed("""
157154 cd /tmp/test-flake
158- CI=1 FE_LOG=debug flake-edit add project1 git+http://forge:3000/test/project1 --no-flake 2>&1
155+ flake-edit add project1 git+http://forge:3000/test/project1 --no-flake 2>&1
159156 """)
160157 print(f"flake-edit add output: {output}")
161158
162- # Verify the input was added without a version pin
163- flake_content = client.succeed("""
164- cat /tmp/test-flake/flake.nix
165- """)
159+ flake_content = client.succeed("cat /tmp/test-flake/flake.nix")
166160 print(f"flake.nix after add: {flake_content}")
167161 assert "project1" in flake_content, "project1 should be added"
168162
169163 with subtest("Pin input to latest version with update --init"):
170164 output = client.succeed("""
171165 cd /tmp/test-flake
172- CI=1 FE_LOG=debug flake-edit update project1 --init 2>&1
166+ flake-edit update project1 --init 2>&1
173167 """)
174168 print(f"flake-edit update --init output: {output}")
175169
176- # Verify the input was pinned with refs/tags/ prefix
177- flake_content = client.succeed("""
178- cat /tmp/test-flake/flake.nix
179- """)
170+ flake_content = client.succeed("cat /tmp/test-flake/flake.nix")
180171 print(f"flake.nix after update --init: {flake_content}")
181172 assert "refs/tags/v1.0.0" in flake_content, "Should be pinned to refs/tags/v1.0.0"
182173
183174 with subtest("Create additional releases (v1.5.0, v2.0.0)"):
184- # Create v2.0.0
185175 forge.succeed(
186176 "curl --fail -X POST http://localhost:3000/api/v1/repos/test/project1/releases "
187177 + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
188178 + f"-H 'Authorization: token {api_token}'"
189179 + ' -d \'{"tag_name":"v2.0.0","name":"Release v2.0.0","body":"Test release 2.0.0"}\'''
190180 )
191- # Create v1.5.0 (to test sorting)
192181 forge.succeed(
193182 "curl --fail -X POST http://localhost:3000/api/v1/repos/test/project1/releases "
194183 + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
@@ -197,27 +186,172 @@ _: {
197186 )
198187
199188 with subtest("Test flake-edit update to latest version"):
200- # Update project1 to latest (should go to v2.0.0)
201189 output = client.succeed("""
202190 cd /tmp/test-flake
203- CI=1 FE_LOG=debug flake-edit update project1 2>&1
191+ flake-edit update project1 2>&1
204192 """)
205193 print(f"flake-edit update output: {output}")
206194
207- # Check the flake.nix was updated
208- updated_flake = client.succeed("""
209- cat /tmp/test-flake/flake.nix
210- """)
195+ updated_flake = client.succeed("cat /tmp/test-flake/flake.nix")
211196 print(f"Updated flake.nix: {updated_flake}")
212-
213- # Verify it updated to v2.0.0
214- assert "refs/tags/v2.0.0" in updated_flake, "flake.nix should reference refs/tags/v2.0.0"
215- assert "v1.0.0" not in updated_flake, "flake.nix should no longer reference v1.0.0"
197+ assert "refs/tags/v2.0.0" in updated_flake, "Should be updated to v2.0.0"
198+ assert "v1.0.0" not in updated_flake, "Should no longer reference v1.0.0"
216199
217200 with subtest("Verify flake-edit detected Forgejo correctly"):
218- # The update should have worked, which means Forgejo was detected
219- # Check the logs for detection messages (optional, we already verified it worked)
220201 print("flake-edit successfully updated from Forgejo instance")
202+
203+ with subtest("Create 'nixos' organization for channel tests"):
204+ forge.succeed(
205+ "curl --fail -X POST http://localhost:3000/api/v1/orgs "
206+ + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
207+ + f"-H 'Authorization: token {api_token}'"
208+ + ' -d \'{"username":"nixos","full_name":"NixOS","visibility":"public"}\'''
209+ )
210+
211+ with subtest("Create 'nixpkgs' repository under 'nixos' org"):
212+ forge.succeed(
213+ "curl --fail -X POST http://localhost:3000/api/v1/orgs/nixos/repos "
214+ + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
215+ + f"-H 'Authorization: token {api_token}'"
216+ + ' -d \'{"auto_init":true, "name":"nixpkgs", "private":false, "default_branch":"nixos-unstable"}\'''
217+ )
218+
219+ with subtest("Create channel branches for nixpkgs"):
220+ default_sha = forge.succeed(
221+ "curl -sfS http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches/nixos-unstable "
222+ + f"-H 'Authorization: token {api_token}' "
223+ + "| jq -r '.commit.id' | xargs echo -n"
224+ )
225+ forge.succeed(
226+ "curl --fail -X POST http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches "
227+ + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
228+ + f"-H 'Authorization: token {api_token}' "
229+ + f'-d \'{{"new_branch_name":"nixos-24.05","old_ref_name":"{default_sha}"}}\'''
230+ )
231+
232+ with subtest("Verify nixpkgs branches exist"):
233+ branches = client.succeed("""
234+ curl -sfS http://forge:3000/api/v1/repos/nixos/nixpkgs/branches \
235+ | jq -r '.[].name' | sort
236+ """)
237+ print(f"Available branches: {branches}")
238+ assert "nixos-24.05" in branches, "nixos-24.05 branch should exist"
239+ assert "nixos-unstable" in branches, "nixos-unstable branch should exist"
240+
241+ with subtest("Create test flake for channel tests"):
242+ write_empty_flake(client, "/tmp/channel-test/flake.nix")
243+ client.succeed("cat /tmp/channel-test/flake.nix")
244+
245+ with subtest("Add nixpkgs input without version pin"):
246+ output = client.succeed("""
247+ cd /tmp/channel-test
248+ flake-edit add nixpkgs git+http://forge:3000/nixos/nixpkgs --no-flake 2>&1
249+ """)
250+ print(f"flake-edit add nixpkgs output: {output}")
251+
252+ flake_content = client.succeed("cat /tmp/channel-test/flake.nix")
253+ print(f"flake.nix after adding nixpkgs: {flake_content}")
254+ assert "nixpkgs" in flake_content, "nixpkgs should be added"
255+ assert "nixos-24" not in flake_content, "Should not have a channel ref yet (unpinned)"
256+
257+ with subtest("Channel update --init on unpinned input"):
258+ output = client.succeed("""
259+ cd /tmp/channel-test
260+ flake-edit update nixpkgs --init 2>&1
261+ """)
262+ print(f"flake-edit update --init output: {output}")
263+ flake_content = client.succeed("cat /tmp/channel-test/flake.nix")
264+ print(f"flake.nix after update --init: {flake_content}")
265+
266+ with subtest("Set nixpkgs to nixos-24.05 channel"):
267+ write_empty_flake(client, "/tmp/channel-test/flake.nix")
268+ client.succeed("""
269+ cd /tmp/channel-test
270+ flake-edit add nixpkgs 'git+http://forge:3000/nixos/nixpkgs?ref=nixos-24.05' --no-flake
271+ """)
272+ client.succeed("cat /tmp/channel-test/flake.nix")
273+
274+ with subtest("Create nixos-24.11 branch"):
275+ branch_sha = forge.succeed(
276+ "curl -sfS http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches/nixos-24.05 "
277+ + f"-H 'Authorization: token {api_token}' "
278+ + "| jq -r '.commit.id' | xargs echo -n"
279+ )
280+ forge.succeed(
281+ "curl --fail -X POST http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches "
282+ + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
283+ + f"-H 'Authorization: token {api_token}' "
284+ + f'-d \'{{"new_branch_name":"nixos-24.11","old_ref_name":"{branch_sha}"}}\'''
285+ )
286+ branches = client.succeed("""
287+ curl -sfS http://forge:3000/api/v1/repos/nixos/nixpkgs/branches \
288+ | jq -r '.[].name' | sort
289+ """)
290+ print(f"Branches: {branches}")
291+ assert "nixos-24.11" in branches, "nixos-24.11 branch should exist"
292+
293+ with subtest("Channel update should upgrade from 24.05 to 24.11"):
294+ output = client.succeed("""
295+ cd /tmp/channel-test
296+ flake-edit update nixpkgs 2>&1
297+ """)
298+ print(f"flake-edit channel update output: {output}")
299+
300+ flake_content = client.succeed("cat /tmp/channel-test/flake.nix")
301+ print(f"flake.nix after channel update: {flake_content}")
302+ assert "nixos-24.11" in flake_content, "Should be updated to nixos-24.11"
303+ assert "nixos-24.05" not in flake_content, "Should no longer reference nixos-24.05"
304+
305+ with subtest("Verify unstable channels are not updated"):
306+ write_empty_flake(client, "/tmp/channel-test/flake.nix")
307+ client.succeed("""
308+ cd /tmp/channel-test
309+ flake-edit add nixpkgs 'git+http://forge:3000/nixos/nixpkgs?ref=nixos-unstable' --no-flake
310+ """)
311+ output = client.succeed("""
312+ cd /tmp/channel-test
313+ flake-edit update nixpkgs 2>&1
314+ """)
315+ print(f"flake-edit update on unstable output: {output}")
316+
317+ flake_content = client.succeed("cat /tmp/channel-test/flake.nix")
318+ print(f"flake.nix after update on unstable: {flake_content}")
319+ assert "nixos-unstable" in flake_content, "Should remain on nixos-unstable"
320+ assert "nixos-24" not in flake_content, "Should NOT be changed to a stable channel"
321+
322+ with subtest("Verify nixpkgs- prefix channels also work"):
323+ branch_sha = forge.succeed(
324+ "curl -sfS http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches/nixos-unstable "
325+ + f"-H 'Authorization: token {api_token}' "
326+ + "| jq -r '.commit.id' | xargs echo -n"
327+ )
328+ forge.succeed(
329+ "curl --fail -X POST http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches "
330+ + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
331+ + f"-H 'Authorization: token {api_token}' "
332+ + f'-d \'{{"new_branch_name":"nixpkgs-24.05","old_ref_name":"{branch_sha}"}}\'''
333+ )
334+ forge.succeed(
335+ "curl --fail -X POST http://localhost:3000/api/v1/repos/nixos/nixpkgs/branches "
336+ + "-H 'Accept: application/json' -H 'Content-Type: application/json' "
337+ + f"-H 'Authorization: token {api_token}' "
338+ + f'-d \'{{"new_branch_name":"nixpkgs-24.11","old_ref_name":"{branch_sha}"}}\'''
339+ )
340+
341+ write_empty_flake(client, "/tmp/channel-test/flake.nix")
342+ client.succeed("""
343+ cd /tmp/channel-test
344+ flake-edit add nixpkgs 'git+http://forge:3000/nixos/nixpkgs?ref=nixpkgs-24.05' --no-flake
345+ """)
346+ output = client.succeed("""
347+ cd /tmp/channel-test
348+ flake-edit update nixpkgs 2>&1
349+ """)
350+ print(f"flake-edit update nixpkgs- prefix output: {output}")
351+
352+ flake_content = client.succeed("cat /tmp/channel-test/flake.nix")
353+ print(f"flake.nix after nixpkgs- update: {flake_content}")
354+ assert "nixpkgs-24.11" in flake_content, "Should be updated to nixpkgs-24.11"
221355 '' ;
222356 } ;
223357 } ;
0 commit comments