summaryrefslogtreecommitdiffstats
path: root/pkgs/build-support/src-only/tests.nix
blob: 039fd9eb9bb43ce7fbaa6fe4178aa780e8832530 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
{
  lib,
  runCommand,
  srcOnly,
  hello,
  emptyDirectory,
  zlib,
  git,
  withCFlags,
  stdenv,
  testers,
}:

let
  # Extract (effective) arguments passed to stdenv.mkDerivation and compute the
  # arguments we would need to pass to srcOnly manually in order to get the same
  # as `srcOnly drv`, i.e. the arguments passed to stdenv.mkDerivation plus the
  # used stdenv itself.
  getEquivAttrs =
    drv:
    let
      drv' = drv.overrideAttrs (
        _finalAttrs: prevAttrs: {
          passthru = prevAttrs.passthru or { } // {
            passedAttrs = prevAttrs;
          };
        }
      );
    in
    drv'.passedAttrs // { inherit (drv') stdenv; };

  canEvalDrv = drv: (builtins.tryEval drv.drvPath).success;

  emptySrc = srcOnly emptyDirectory;
  zlibSrc = srcOnly zlib;

  # It can be invoked in a number of ways. Let's make sure they're equivalent.
  zlibSrcEquiv = srcOnly (getEquivAttrs zlib);
  # zlibSrcFreeform = # ???;
  helloSrc = srcOnly hello;
  helloSrcEquiv = srcOnly (getEquivAttrs hello);

  gitSrc = srcOnly git;
  gitSrcEquiv = srcOnly (getEquivAttrs git);

  # The srcOnly <drv> invocation leaks a lot of attrs into the srcOnly derivation,
  # so for comparing with the freeform invocation, we need to make a selection.
  # Otherwise, we'll be comparing against whatever attribute the fancy hello drv
  # has.
  helloDrvSimple = stdenv.mkDerivation {
    inherit (hello)
      name
      pname
      version
      src
      patches
      ;
  };
  helloDrvSimpleSrc = srcOnly helloDrvSimple;
  helloDrvSimpleSrcFreeform = srcOnly {
    inherit (helloDrvSimple)
      name
      pname
      version
      src
      patches
      stdenv
      ;
  };

  # Test the issue reported in https://github.com/NixOS/nixpkgs/issues/269539
  stdenvAdapterDrv =
    let
      drv = (withCFlags [ "-Werror" "-Wall" ] stdenv).mkDerivation {
        name = "drv-using-stdenv-adapter";
      };
    in
    # Confirm the issue we are trying to avoid exists
    assert !(canEvalDrv (srcOnly drv.drvAttrs));
    drv;
  stdenvAdapterDrvSrc = srcOnly stdenvAdapterDrv;
  stdenvAdapterDrvSrcEquiv = srcOnly (
    getEquivAttrs stdenvAdapterDrv
    // {
      # The upside of using overrideAttrs is that any stdenv adapter related
      # modifications are only applied once. Using the adapter here again would
      # mean applying it twice in total (since withCFlags functions more or less
      # like an automatic overrideAttrs).
      inherit stdenv;
    }
  );

  # Issue similar to https://github.com/NixOS/nixpkgs/issues/269539
  structuredAttrsDrv =
    let
      drv = stdenv.mkDerivation {
        name = "drv-using-structured-attrs";
        src = emptyDirectory;

        env.NIX_DEBUG = true;
        __structuredAttrs = true;
      };
    in
    # Confirm the issue we are trying to avoid exists
    assert !(canEvalDrv (srcOnly drv.drvAttrs));
    drv;
  structuredAttrsDrvSrc = srcOnly structuredAttrsDrv;
  structuredAttrsDrvSrcEquiv = srcOnly (getEquivAttrs structuredAttrsDrv);

in

runCommand "srcOnly-tests"
  {
    moreTests = [
      (testers.testEqualDerivation "zlibSrcEquiv == zlibSrc" zlibSrcEquiv zlibSrc)
      # (testers.testEqualDerivation
      #   "zlibSrcFreeform == zlibSrc"
      #   zlibSrcFreeform
      #   zlibSrc)
      (testers.testEqualDerivation "helloSrcEquiv == helloSrc" helloSrcEquiv helloSrc)
      (testers.testEqualDerivation "helloSrcEquiv == helloSrc" helloSrcEquiv helloSrc)
      (testers.testEqualDerivation "gitSrcEquiv == gitSrc" gitSrcEquiv gitSrc)
      (testers.testEqualDerivation "helloDrvSimpleSrcFreeform == helloDrvSimpleSrc"
        helloDrvSimpleSrcFreeform
        helloDrvSimpleSrc
      )
      (testers.testEqualDerivation "stdenvAdapterDrvSrcEquiv == stdenvAdapterDrvSrc"
        stdenvAdapterDrvSrcEquiv
        stdenvAdapterDrvSrc
      )
      (testers.testEqualDerivation "structuredAttrsDrvSrcEquiv == structuredAttrsDrvSrc"
        structuredAttrsDrvSrcEquiv
        structuredAttrsDrvSrc
      )
    ];
  }
  ''
    # Test that emptySrc is empty
    if [ -n "$(ls -A ${emptySrc})" ]; then
      echo "emptySrc is not empty"
      exit 1
    fi

    # Test that zlibSrc is not empty
    if [ -z "$(ls -A ${zlibSrc})" ]; then
      echo "zlibSrc is empty"
      exit 1
    fi

    # Make $out exist to avoid build failure
    mkdir -p $out
  ''