about summary refs log tree commit diff
path: root/lib/fileset/mock-splitRoot.nix
blob: 3c18ab1b1afd2071646f8b6dad61be8dc340bf58 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# This overlay implements mocking of the lib.path.splitRoot function
# It pretends that the last component named "mock-root" is the root:
#
# splitRoot /foo/mock-root/bar/mock-root/baz
# => {
#      root = /foo/mock-root/bar/mock-root;
#      subpath = "./baz";
#    }
self: super: {
  path = super.path // {
    splitRoot = path:
      let
        parts = super.path.splitRoot path;
        components = self.path.subpath.components parts.subpath;
        count = self.length components;
        rootIndex = count - self.lists.findFirstIndex
          (component: component == "mock-root")
          (self.length components)
          (self.reverseList components);
        root = self.path.append parts.root (self.path.subpath.join (self.take rootIndex components));
        subpath = self.path.subpath.join (self.drop rootIndex components);
      in {
        inherit root subpath;
      };
  };
}