nix-helpers: 3db9aaf62cff6cb22f2957c09ebfcc3d80554c0a

     1: { bash, hello, jq, lib, python, python3, runCommand, stdenv, wrap, writeScript
     2: }:
     3: 
     4: with lib;
     5: with {
     6:   # Make sure that derivations given as paths and vars aren't forced during
     7:   # evaluation (only at build time)
     8:   depChk = with {
     9:     script = wrap {
    10:       name = "depChk-script";
    11:       vars = { broken1 = runCommand "broken1" { } "exit 1"; };
    12:       paths = [ (runCommand "broken2" { } "exit 1") ];
    13:       script = "exit 1";
    14:     };
    15:   }; {
    16:     brokenDepsNotForced = runCommand "checkBrokenDepsNotForced" {
    17:       val = if isString script.buildCommand then "true" else "false";
    18:     } ''
    19:       if "$val"
    20:       then
    21:         echo "pass" > "$out"
    22:         exit 0
    23:       fi
    24:       exit 1
    25:     '';
    26: 
    27:     haveDeps = runCommand "checkHaveDeps" {
    28:       script = wrap {
    29:         name = "haveDepsChecker";
    30:         vars = {
    31:           A = "foo";
    32:           B = "hello world";
    33:           C = "Single 'quotes'";
    34:           D = ''Double "quotes"'';
    35:         };
    36:         paths = [ jq python ];
    37:         script = ''
    38:           #!${bash}/bin/bash
    39:           command -v jq || {
    40:             echo "No jq" 1>&2
    41:             exit 1
    42:           }
    43: 
    44:           command -v python || {
    45:             echo "No python" 1>&2
    46:             exit 1
    47:           }
    48: 
    49:           [[ "x$A" = "xfoo" ]] || {
    50:             echo "No A?" 1>&2
    51:             env 1>&2
    52:             exit 1
    53:           }
    54: 
    55:           [[ "x$B" = "xhello world" ]] || {
    56:             echo "No B?" 1>&2
    57:             env 1>&2
    58:             exit 1
    59:           }
    60: 
    61:           [[ "x$C" = "xSingle 'quotes'" ]] || {
    62:             echo "No C?" 1>&2
    63:             env 1>&2
    64:             exit 1
    65:           }
    66: 
    67:           [[ "x$D" = 'xDouble "quotes"' ]] || {
    68:             echo "No D?" 1>&2
    69:             env 1>&2
    70:             exit 1
    71:           }
    72: 
    73:           echo "pass" > "$out"
    74:         '';
    75:       };
    76:     } ''"$script"'';
    77:   };
    78: 
    79:   # Try a bunch of strings with quotes, spaces, etc. and see if they survive
    80:   varChk = mapAttrs (n: v:
    81:     runCommand "wrap-escapes-${n}" {
    82:       cmd = wrap rec {
    83:         vars = { "${n}" = v; };
    84:         name = "check-wrap-escaping-${n}";
    85:         paths = [ python ];
    86:         script = ''
    87:           #!${python}/bin/python
    88:           from os import getenv
    89: 
    90:           n   = '${n}'
    91:           v   = """${v}"""
    92:           msg = "'{0}' was '{1}' not '{2}'"
    93:           env = getenv(n)
    94: 
    95:           assert env == v, msg.format(n, env, v)
    96: 
    97:           print 'true'
    98:         '';
    99:       };
   100:     } ''"$cmd" > "$out"'') {
   101:       SIMPLE = "simple";
   102:       SPACES = "with some spaces";
   103:       SINGLE = "withA'Quote";
   104:       DOUBLE = ''withA"Quote'';
   105:       MEDLEY = ''with" all 'of the" above'';
   106:     };
   107: 
   108:   wrapChk = {
   109:     # Ensure files and scripts don't get unneeded wrappers if no env is given
   110:     unwrappedFile = runCommand "unwrappedFile" {
   111:       val = wrap {
   112:         name = "foo";
   113:         file = writeScript "bar" "baz";
   114:       };
   115:     } ''
   116:       [[ -e "$val" ]] || {
   117:         echo "No such file '$val'" 1>&2
   118:         exit 1
   119:       }
   120:       [[ -h "$val" ]] || {
   121:         echo "Not a link '$val'" 1>&2
   122:         exit 1;
   123:       }
   124:       echo pass > "$out"
   125:     '';
   126: 
   127:     unwrappedScript = runCommand "unwrappedScript" {
   128:       val = wrap {
   129:         name = "foo";
   130:         script = "bar";
   131:       };
   132:     } ''
   133:       [[ -e "$val" ]] || {
   134:         echo "No such file '$val'" 1>&2
   135:         exit 1
   136:       }
   137:       [[ -h "$val" ]] || {
   138:         echo "Not a link '$val'" 1>&2
   139:         exit 1;
   140:       }
   141:       echo pass > "$out"
   142:     '';
   143:   };
   144: 
   145:   # Check that propagated dependencies get included
   146:   propCheck = {
   147:     oneLevel = runCommand "checkDirectPropagationOfWrappedDeps" {
   148:       wrapped = wrap {
   149:         name = "accessPropagated1";
   150:         paths = [
   151:           (stdenv.mkDerivation {
   152:             name = "dummy";
   153:             src = ./default.nix;
   154:             propagatedBuildInputs = [ hello ];
   155:             installPhase = ''mkdir "$out"'';
   156:             unpackPhase = "true";
   157:           })
   158:         ];
   159:         script = ''
   160:           #!${bash}/bin/bash
   161:           set -e
   162:           command -v hello || {
   163:             echo "Program 'hello' not found in PATH ($PATH)" 1>&2
   164:             exit 1
   165:           }
   166:           echo "Found 'hello' command" 1>&2
   167:           exit 0
   168:         '';
   169:       };
   170:     } ''
   171:       "$wrapped" || exit 1
   172:       mkdir "$out"
   173:     '';
   174: 
   175:     nested = runCommand "checkNestedPropagationOfWrappedDeps" {
   176:       wrapped = wrap {
   177:         name = "accessPropagated2";
   178:         paths = [
   179:           (stdenv.mkDerivation {
   180:             name = "dummy1";
   181:             src = ./default.nix;
   182:             propagatedBuildInputs = [
   183:               (stdenv.mkDerivation {
   184:                 name = "dummy2";
   185:                 src = ./default.nix;
   186:                 propagatedBuildInputs = [
   187:                   (stdenv.mkDerivation {
   188:                     name = "dummy3";
   189:                     src = ./default.nix;
   190:                     propagatedBuildInputs = [ hello ];
   191:                     installPhase = ''mkdir "$out"'';
   192:                     unpackPhase = "true";
   193:                   })
   194:                 ];
   195:                 installPhase = ''mkdir "$out"'';
   196:                 unpackPhase = "true";
   197:               })
   198:             ];
   199:             installPhase = ''mkdir "$out"'';
   200:             unpackPhase = "true";
   201:           })
   202:         ];
   203:         script = ''
   204:           #!${bash}/bin/bash
   205:           set -e
   206:           command -v hello || {
   207:             echo "Program 'hello' not found in PATH ($PATH)" 1>&2
   208:             exit 1
   209:           }
   210:           echo "Found 'hello' command" 1>&2
   211:           exit 0
   212:         '';
   213:       };
   214:     } ''
   215:       "$wrapped" || exit 1
   216:       mkdir "$out"
   217:     '';
   218:   };
   219: };
   220: varChk // depChk // wrapChk // propCheck // {
   221:   wrap-test = wrap {
   222:     name = "wrap-test";
   223:     paths = [ bash ];
   224:     vars = { MY_VAR = "MY VAL"; };
   225:     script = ./default.nix;
   226:   };
   227: 
   228:   python-test = runCommand "wrap-python-test" {
   229:     script = wrap {
   230:       name = "wrap-python-test.py";
   231:       paths = [ python3 ];
   232:       script = ''
   233:         #!${python3}/bin/python3
   234:         from subprocess import Popen, PIPE
   235:         p = Popen(['cat'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
   236:         i = b'foo'
   237:         (sout, serr) = p.communicate(i)
   238:         assert sout.strip() == i, repr({
   239:           'error'  : 'Output of cat did not match input',
   240:           'input'  : i,
   241:           'stdout' : sout,
   242:           'stderr' : serr
   243:         })
   244:       '';
   245:     };
   246:   } ''
   247:     "$script" || {
   248:       echo "Wrapped Python script failed" 1>&2
   249:       exit 1
   250:     }
   251:     mkdir "$out"
   252:   '';
   253: }

Generated by git2html.