Skip to content

Commit

Permalink
tests: add unit tests for modules/ref.nix.
Browse files Browse the repository at this point in the history
  • Loading branch information
gmodena committed Oct 13, 2024
1 parent 0079254 commit ca057f1
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 25 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ jobs:
with:
github_access_token: ${{ secrets.GITHUB_TOKEN }}
- run: nix flake check
- name: Run Nix tests
run: |
cd tests/
result=$(nix eval --impure --expr 'import ./ref-test.nix {}')
if [ "$result" != "[ ]" ]; then
echo "Test failed: Expected [], but got $result"
exit 1
fi
shell: bash
24 changes: 2 additions & 22 deletions modules/installer.nix
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{ cfg, pkgs, lib, installation ? "system", ... }:

let
utils = import ./ref.nix { inherit pkgs lib; };
utils = import ./ref.nix { inherit lib; };

flatpakrefCache = builtins.foldl'
(acc: package:
Expand All @@ -10,26 +10,6 @@ let
{ }
(builtins.filter (package: utils.isFlatpakref package) cfg.packages);

# Extract the remote name from a package that declares a flatpakref:
# 1. if the package sets an origin, use that as label for the remote url.
# 2. if the package does not set an origin, use the remote name suggested by the flatpakref.
# 3. if the package does not set an origin and the flatpakref does not suggest a remote name, sanitize application Name.
getRemoteNameFromFlatpakref = origin: cache:
let
remoteName = origin;
in
if remoteName == null
then
let
flatpakrefdName =
if builtins.hasAttr "SuggestRemoteName" cache
then cache.SuggestRemoteName
else "${lib.toLower cache.Name}-origin";
in
flatpakrefdName
else
remoteName;

# Get the appId from the flatpakref file or the flatpakref URL to pass to flatpak commands.
# As of 2024-10 Flatpak will fail to reinstall from flatpakref URL (https://github.com/flatpak/flatpak/issues/5460).
# This function will return the appId if the package is already installed, otherwise it will return the flatpakref URL.
Expand Down Expand Up @@ -84,7 +64,7 @@ let
# Add remotes extracted from flatpakref URLs in packages
map
(package:
getRemoteNameFromFlatpakref package.origin flatpakrefCache.${(utils.sanitizeUrl package.flatpakref)})
utils.getRemoteNameFromFlatpakref package.origin flatpakrefCache.${(utils.sanitizeUrl package.flatpakref)})
(builtins.filter (package: utils.isFlatpakref package) cfg.packages);
});

Expand Down
26 changes: 23 additions & 3 deletions modules/ref.nix
Original file line number Diff line number Diff line change
@@ -1,16 +1,36 @@
# utiliy function to manage flatpakref files
{ pkgs, lib, ... }:
{ lib, ... }:
let
# check if a value is a string
isString = value: builtins.typeOf value == "string";

# Check if a package declares a flatpakref
isFlatpakref = { flatpakref, ... }:
isFlatpakref = { flatpakref ? null, ... }:
flatpakref != null && isString flatpakref;

# sanitize a URL to be used as a key in an attrset.
sanitizeUrl = url: builtins.replaceStrings [ "https://" "/" "." ":" ] [ "https_" "_" "_" "_" ] url;

# Extract the remote name from a package that declares a flatpakref:
# 1. if the package sets an origin, use that as label for the remote url.
# 2. if the package does not set an origin, use the remote name suggested by the flatpakref.
# 3. if the package does not set an origin and the flatpakref does not suggest a remote name, sanitize application Name.
getRemoteNameFromFlatpakref = origin: cache:
let
remoteName = origin;
in
if remoteName == null
then
let
flatpakrefdName =
if builtins.hasAttr "SuggestRemoteName" cache
then cache.SuggestRemoteName
else "${lib.toLower cache.Name}-origin";
in
flatpakrefdName
else
remoteName;

# Fetch and convert an ini-like flatpakref file into an attrset, and cache it for future use
# within the same activation.
# We piggyback on builtins.fetchurl to fetch and cache flatpakref file. Pure nix evaluations
Expand Down Expand Up @@ -39,5 +59,5 @@ let
updatedCache;
in
{
inherit isFlatpakref sanitizeUrl flatpakrefToAttrSet;
inherit isFlatpakref sanitizeUrl flatpakrefToAttrSet getRemoteNameFromFlatpakref;
}
8 changes: 8 additions & 0 deletions tests/fixtures/package.flatpakref
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[Flatpak Ref]
Title=gedit
Name=org.gnome.gedit
Branch=stable
Url=http://sdk.gnome.org/repo-apps/
IsRuntime=false
GPGKey=REDACTED
DeployCollectionID=org.gnome.Apps
77 changes: 77 additions & 0 deletions tests/ref-test.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
{ pkgs ? import <nixpkgs> { } }:

let
inherit (pkgs) lib;
inherit (lib) runTests;
ref = import ../modules/ref.nix { inherit lib; };

pwd = builtins.getEnv "PWD";
fixturePath = "file://${pwd}/fixtures/package.flatpakref";
fixtureHash = "040iig2yg2i28s5xc9cvp5syaaqq165idy3nhlpv8xn4f6zh4h1f";
in
runTests {
testSanitizeUrl = {
expr = ref.sanitizeUrl "https://example.local";
expected = "https_example_local";
};

testIsFlatpakref = {
expr = ref.isFlatpakref { flatpakref = "https://example.local/package.flatpakref"; };
expected = true;
};

testIsFlatpakrefWithNull = {
expr = ref.isFlatpakref { flatpakref = null; };
expected = false;
};

testIsFlatpakrefWithMissing = {
expr = ref.isFlatpakref { appId = "local.example.Package"; };
expected = false;
};

testGetRemoteNameFromFlatpakrefWithOrigin = {
expr = ref.getRemoteNameFromFlatpakref "example" { SuggestRemoteName = "local"; };
expected = "example";
};

testGetRemoteNameWithSuggestedName = {
expr = ref.getRemoteNameFromFlatpakref null { SuggestRemoteName = "local"; };
expected = "local";
};

testGetRemoteNameWithPackageName = {
expr = ref.getRemoteNameFromFlatpakref null { Name = "Example"; };
expected = "example-origin";
};

testFlatpakrefToAttrSet = {
expr = ref.flatpakrefToAttrSet { flatpakref = fixturePath; sha256 = null; } { };
expected = {
${ref.sanitizeUrl fixturePath} = {
Title = "gedit";
Name = "org.gnome.gedit";
Branch = "stable";
Url = "http://sdk.gnome.org/repo-apps/";
IsRuntime = "false";
GPGKey = "REDACTED";
DeployCollectionID = "org.gnome.Apps";
};
};
};

testFlatpakrefToAttrSetWithSha256 = {
expr = ref.flatpakrefToAttrSet { flatpakref = fixturePath; sha256 = fixtureHash; } { };
expected = {
${ref.sanitizeUrl fixturePath} = {
Title = "gedit";
Name = "org.gnome.gedit";
Branch = "stable";
Url = "http://sdk.gnome.org/repo-apps/";
IsRuntime = "false";
GPGKey = "REDACTED";
DeployCollectionID = "org.gnome.Apps";
};
};
};
}

0 comments on commit ca057f1

Please sign in to comment.