diff options
Diffstat (limited to 'nixpkgs/pkgs/test')
32 files changed, 995 insertions, 0 deletions
diff --git a/nixpkgs/pkgs/test/cc-wrapper/cc-main.c b/nixpkgs/pkgs/test/cc-wrapper/cc-main.c new file mode 100644 index 000000000000..06f28bc33c69 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/cc-main.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + fprintf(stderr, "ok\n"); + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/cflags-main.c b/nixpkgs/pkgs/test/cc-wrapper/cflags-main.c new file mode 100644 index 000000000000..9491232b5387 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/cflags-main.c @@ -0,0 +1,10 @@ +#include <stdio.h> +#include <foo.h> + +int main(int argc, char **argv) +{ + if (foo() != 42) + return 1; + fprintf(stderr, "ok\n"); + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/core-foundation-main.c b/nixpkgs/pkgs/test/cc-wrapper/core-foundation-main.c new file mode 100644 index 000000000000..fb3bd3126191 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/core-foundation-main.c @@ -0,0 +1,7 @@ +#include <CoreFoundation/CoreFoundation.h> + +int main(int argc, char** argv) +{ + CFShow(CFSTR("ok")); + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/cxx-main.cc b/nixpkgs/pkgs/test/cc-wrapper/cxx-main.cc new file mode 100644 index 000000000000..83f704617a46 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/cxx-main.cc @@ -0,0 +1,7 @@ +#include <iostream> + +int main(int argc, char **argv) +{ + std::cerr << "ok" << std::endl; + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/default.nix b/nixpkgs/pkgs/test/cc-wrapper/default.nix new file mode 100644 index 000000000000..c0c89d63ffff --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/default.nix @@ -0,0 +1,66 @@ +{ stdenv }: +with stdenv.lib; +let + # Sanitizers are not supported on Darwin. + # Sanitizer headers aren't available in older libc++ stdenvs due to a bug + sanitizersWorking = + (stdenv.cc.isClang && versionAtLeast (getVersion stdenv.cc.name) "5.0.0") + || (stdenv.cc.isGNU && stdenv.isLinux); +in stdenv.mkDerivation { + name = "cc-wrapper-test"; + + buildCommand = '' + NIX_DEBUG=1 $CC -v + NIX_DEBUG=1 $CXX -v + + printf "checking whether compiler builds valid C binaries... " >&2 + $CC -o cc-check ${./cc-main.c} + ./cc-check + + printf "checking whether compiler builds valid C++ binaries... " >&2 + $CXX -o cxx-check ${./cxx-main.cc} + ./cxx-check + + ${optionalString (stdenv.isDarwin && stdenv.cc.isClang) '' + printf "checking whether compiler can build with CoreFoundation.framework... " >&2 + mkdir -p foo/lib + $CC -framework CoreFoundation -o core-foundation-check ${./core-foundation-main.c} + ./core-foundation-check + ''} + + printf "checking whether compiler uses NIX_CFLAGS_COMPILE... " >&2 + mkdir -p foo/include + cp ${./foo.c} foo/include/foo.h + NIX_CFLAGS_COMPILE="-Ifoo/include -DVALUE=42" $CC -o cflags-check ${./cflags-main.c} + ./cflags-check + + printf "checking whether compiler uses NIX_LDFLAGS... " >&2 + mkdir -p foo/lib + $CC -shared \ + ${optionalString stdenv.isDarwin "-Wl,-install_name,@rpath/libfoo.dylib"} \ + -DVALUE=42 \ + -o foo/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \ + ${./foo.c} + + NIX_LDFLAGS="-L$NIX_BUILD_TOP/foo/lib -rpath $NIX_BUILD_TOP/foo/lib" $CC -lfoo -o ldflags-check ${./ldflags-main.c} + ./ldflags-check + + printf "Check whether -nostdinc and -nostdinc++ is handled correctly" >&2 + mkdir -p std-include + cp ${./stdio.h} std-include/stdio.h + NIX_DEBUG=1 $CC -I std-include -nostdinc -o nostdinc-main ${./nostdinc-main.c} + ./nostdinc-main + $CXX -I std-include -nostdinc++ -o nostdinc-main++ ${./nostdinc-main.c} + ./nostdinc-main++ + + ${optionalString sanitizersWorking '' + printf "checking whether sanitizers are fully functional... ">&2 + $CC -o sanitizers -fsanitize=address,undefined ${./sanitizers.c} + ./sanitizers + ''} + + touch $out + ''; + + meta.platforms = platforms.all; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/foo.c b/nixpkgs/pkgs/test/cc-wrapper/foo.c new file mode 100644 index 000000000000..8be674be3103 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/foo.c @@ -0,0 +1,4 @@ +unsigned int foo(void) +{ + return VALUE; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/ldflags-main.c b/nixpkgs/pkgs/test/cc-wrapper/ldflags-main.c new file mode 100644 index 000000000000..89832b3bbad2 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/ldflags-main.c @@ -0,0 +1,12 @@ +#include <stdio.h> + +extern unsigned int foo(void); + +int main(int argc, char **argv) +{ + if (foo() != 42) { + return 1; + } + fprintf(stderr, "ok\n"); + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/multilib.nix b/nixpkgs/pkgs/test/cc-wrapper/multilib.nix new file mode 100644 index 000000000000..5ea50b5eb268 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/multilib.nix @@ -0,0 +1,37 @@ +{ stdenv }: + +stdenv.mkDerivation { + name = "cc-multilib-test"; + + # XXX: "depend" on cc-wrapper test? + + # TODO: Have tests report pointer size or something; ensure they are what we asked for + buildCommand = '' + NIX_DEBUG=1 $CC -v + NIX_DEBUG=1 $CXX -v + + printf "checking whether compiler builds valid C binaries... " >&2 + $CC -o cc-check ${./cc-main.c} + ./cc-check + + printf "checking whether compiler builds valid 32bit C binaries... " >&2 + $CC -m32 -o c32-check ${./cc-main.c} + ./c32-check + + printf "checking whether compiler builds valid 64bit C binaries... " >&2 + $CC -m64 -o c64-check ${./cc-main.c} + ./c64-check + + printf "checking whether compiler builds valid 32bit C++ binaries... " >&2 + $CXX -m32 -o cxx32-check ${./cxx-main.cc} + ./cxx32-check + + printf "checking whether compiler builds valid 64bit C++ binaries... " >&2 + $CXX -m64 -o cxx64-check ${./cxx-main.cc} + ./cxx64-check + + touch $out + ''; + + meta.platforms = stdenv.lib.platforms.x86_64; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/nostdinc-main.c b/nixpkgs/pkgs/test/cc-wrapper/nostdinc-main.c new file mode 100644 index 000000000000..f71d155b1b27 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/nostdinc-main.c @@ -0,0 +1,8 @@ +// This one should not come from libc because of -nostdinc +#include <stdio.h> + +int main(int argc, char *argv[]) { + // provided by our own stdio.h + foo(); + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/sanitizers.c b/nixpkgs/pkgs/test/cc-wrapper/sanitizers.c new file mode 100644 index 000000000000..93dd78a903ce --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/sanitizers.c @@ -0,0 +1,8 @@ +#include <sanitizer/asan_interface.h> +#include <stdio.h> + +int main(int argc, char **argv) +{ + fprintf(stderr, "ok\n"); + return 0; +} diff --git a/nixpkgs/pkgs/test/cc-wrapper/stdio.h b/nixpkgs/pkgs/test/cc-wrapper/stdio.h new file mode 100644 index 000000000000..4bddf1d9d486 --- /dev/null +++ b/nixpkgs/pkgs/test/cc-wrapper/stdio.h @@ -0,0 +1 @@ +static void foo(void) {} diff --git a/nixpkgs/pkgs/test/cross/default.nix b/nixpkgs/pkgs/test/cross/default.nix new file mode 100644 index 000000000000..c5a241437732 --- /dev/null +++ b/nixpkgs/pkgs/test/cross/default.nix @@ -0,0 +1,113 @@ +{ pkgs, lib }: + +let + + testedSystems = lib.filterAttrs (name: value: let + platform = lib.systems.elaborate value; + in platform.isLinux || platform.isWindows + ) lib.systems.examples; + + getExecutable = pkgs: pkgFun: exec: + "${pkgFun pkgs}${exec}${pkgs.hostPlatform.extensions.executable}"; + + compareTest = { emulator, pkgFun, hostPkgs, crossPkgs, exec, args ? [] }: let + pkgName = (pkgFun hostPkgs).name; + args' = lib.concatStringsSep " " args; + in crossPkgs.runCommand "test-${pkgName}-${crossPkgs.hostPlatform.config}" { + nativeBuildInputs = [ pkgs.dos2unix ]; + } '' + # Just in case we are using wine, get rid of that annoying extra + # stuff. + export WINEDEBUG=-all + + HOME=$(pwd) + mkdir -p $out + + # We need to remove whitespace, unfortunately + # Windows programs use \r but Unix programs use \n + + echo Running native-built program natively + + # find expected value natively + ${getExecutable hostPkgs pkgFun exec} ${args'} \ + | dos2unix > $out/expected + + echo Running cross-built program in emulator + + # run emulator to get actual value + ${emulator} ${getExecutable crossPkgs pkgFun exec} ${args'} \ + | dos2unix > $out/actual + + echo Comparing results... + + if [ "$(cat $out/actual)" != "$(cat $out/expected)" ]; then + echo "${pkgName} did not output expected value:" + cat $out/expected + echo "instead it output:" + cat $out/actual + exit 1 + else + echo "${pkgName} test passed" + echo "both produced output:" + cat $out/actual + fi + ''; + + mapMultiPlatformTest = crossSystemFun: test: lib.mapAttrs (name: system: test rec { + crossPkgs = import pkgs.path { + localSystem = { inherit (pkgs.hostPlatform) config; }; + crossSystem = crossSystemFun system; + }; + + emulator = crossPkgs.hostPlatform.emulator pkgs; + + # Apply some transformation on windows to get dlls in the right + # place. Unfortunately mingw doesn’t seem to be able to do linking + # properly. + platformFun = pkg: if crossPkgs.hostPlatform.isWindows then + pkgs.buildEnv { + name = "${pkg.name}-winlinks"; + paths = [pkg] ++ pkg.buildInputs; + } else pkg; + }) testedSystems; + + tests = { + + file = {platformFun, crossPkgs, emulator}: compareTest { + inherit emulator crossPkgs; + hostPkgs = pkgs; + exec = "/bin/file"; + args = [ + "${pkgs.file}/share/man/man1/file.1.gz" + "${pkgs.dejavu_fonts}/share/fonts/truetype/DejaVuMathTeXGyre.ttf" + ]; + pkgFun = pkgs: platformFun pkgs.file; + }; + + hello = {platformFun, crossPkgs, emulator}: compareTest { + inherit emulator crossPkgs; + hostPkgs = pkgs; + exec = "/bin/hello"; + pkgFun = pkgs: pkgs.hello; + }; + + pkg-config = {platformFun, crossPkgs, emulator}: crossPkgs.runCommand + "test-pkg-config-${crossPkgs.hostPlatform.config}" + { + depsBuildBuild = [ crossPkgs.pkgsBuildBuild.pkg-config ]; + nativeBuildInputs = [ crossPkgs.pkgsBuildHost.pkg-config crossPkgs.buildPackages.zlib ]; + depsBuildTarget = [ crossPkgs.pkgsBuildTarget.pkg-config ]; + buildInputs = [ crossPkgs.zlib ]; + NIX_DEBUG = 7; + } '' + mkdir $out + ${crossPkgs.pkgsBuildBuild.pkg-config.targetPrefix}pkg-config --cflags zlib > "$out/for-build" + ${crossPkgs.pkgsBuildHost.pkg-config.targetPrefix}pkg-config --cflags zlib > "$out/for-host" + ! diff "$out/for-build" "$out/for-host" + ''; + }; + +in { + gcc = (lib.mapAttrs (_: mapMultiPlatformTest (system: system // {useLLVM = false;})) tests); + llvm = (lib.mapAttrs (_: mapMultiPlatformTest (system: system // {useLLVM = true;})) tests); +} diff --git a/nixpkgs/pkgs/test/default.nix b/nixpkgs/pkgs/test/default.nix new file mode 100644 index 000000000000..c248eebaec39 --- /dev/null +++ b/nixpkgs/pkgs/test/default.nix @@ -0,0 +1,43 @@ +{ pkgs, callPackage }: + +with pkgs; + +{ + cc-wrapper = callPackage ./cc-wrapper { }; + cc-wrapper-gcc = callPackage ./cc-wrapper { stdenv = gccStdenv; }; + cc-wrapper-gcc7 = callPackage ./cc-wrapper { stdenv = gcc7Stdenv; }; + cc-wrapper-gcc8 = callPackage ./cc-wrapper { stdenv = gcc8Stdenv; }; + cc-wrapper-gcc9 = callPackage ./cc-wrapper { stdenv = gcc9Stdenv; }; + cc-wrapper-clang = callPackage ./cc-wrapper { stdenv = llvmPackages.stdenv; }; + cc-wrapper-libcxx = callPackage ./cc-wrapper { stdenv = llvmPackages.libcxxStdenv; }; + cc-wrapper-clang-5 = callPackage ./cc-wrapper { stdenv = llvmPackages_5.stdenv; }; + cc-wrapper-libcxx-5 = callPackage ./cc-wrapper { stdenv = llvmPackages_5.libcxxStdenv; }; + cc-wrapper-clang-6 = callPackage ./cc-wrapper { stdenv = llvmPackages_6.stdenv; }; + cc-wrapper-libcxx-6 = callPackage ./cc-wrapper { stdenv = llvmPackages_6.libcxxStdenv; }; + cc-wrapper-clang-7 = callPackage ./cc-wrapper { stdenv = llvmPackages_7.stdenv; }; + cc-wrapper-libcxx-7 = callPackage ./cc-wrapper { stdenv = llvmPackages_7.libcxxStdenv; }; + cc-wrapper-clang-8 = callPackage ./cc-wrapper { stdenv = llvmPackages_8.stdenv; }; + cc-wrapper-libcxx-8 = callPackage ./cc-wrapper { stdenv = llvmPackages_8.libcxxStdenv; }; + cc-wrapper-clang-9 = callPackage ./cc-wrapper { stdenv = llvmPackages_9.stdenv; }; + cc-wrapper-libcxx-9 = callPackage ./cc-wrapper { stdenv = llvmPackages_9.libcxxStdenv; }; + stdenv-inputs = callPackage ./stdenv-inputs { }; + + haskell-shellFor = callPackage ./haskell-shellFor { }; + + cc-multilib-gcc = callPackage ./cc-wrapper/multilib.nix { stdenv = gccMultiStdenv; }; + cc-multilib-clang = callPackage ./cc-wrapper/multilib.nix { stdenv = clangMultiStdenv; }; + + kernel-config = callPackage ./kernel.nix {}; + + ld-library-path = callPackage ./ld-library-path {}; + + macOSSierraShared = callPackage ./macos-sierra-shared {}; + + cross = callPackage ./cross {}; + + nixos-functions = callPackage ./nixos-functions {}; + + patch-shebangs = callPackage ./patch-shebangs {}; + + writers = callPackage ../build-support/writers/test.nix {}; +} diff --git a/nixpkgs/pkgs/test/haskell-shellFor/default.nix b/nixpkgs/pkgs/test/haskell-shellFor/default.nix new file mode 100644 index 000000000000..05d09d6f39cf --- /dev/null +++ b/nixpkgs/pkgs/test/haskell-shellFor/default.nix @@ -0,0 +1,33 @@ +{ lib, stdenv, haskellPackages, cabal-install }: + +(haskellPackages.shellFor { + packages = p: [ p.database-id-class p.constraints ]; + nativeBuildInputs = [ cabal-install ]; + phases = [ "unpackPhase" "buildPhase" "installPhase" ]; + unpackPhase = '' + sourceRoot=$(pwd)/scratch + mkdir -p "$sourceRoot" + cd "$sourceRoot" + tar -xf ${haskellPackages.database-id-class.src} + tar -xf ${haskellPackages.constraints.src} + cp ${builtins.toFile "cabal.project" "packages: database-id-class* constraints*"} cabal.project + ''; + buildPhase = '' + export HOME=$(mktemp -d) + mkdir -p $HOME/.cabal + touch $HOME/.cabal/config + cabal v2-build --offline --verbose database-id-class constraints --ghc-options="-O0 -j$NIX_BUILD_CORES" + ''; + installPhase = '' + touch $out + ''; +}).overrideAttrs (oldAttrs: { + meta = + let + oldMeta = oldAttrs.meta or {}; + oldMaintainers = oldMeta.maintainers or []; + additionalMaintainers = with lib.maintainers; [ cdepillabout ]; + allMaintainers = oldMaintainers ++ additionalMaintainers; + in + oldMeta // { maintainers = allMaintainers; }; +}) diff --git a/nixpkgs/pkgs/test/kernel.nix b/nixpkgs/pkgs/test/kernel.nix new file mode 100644 index 000000000000..a4da10030332 --- /dev/null +++ b/nixpkgs/pkgs/test/kernel.nix @@ -0,0 +1,79 @@ +# to run these tests: +# nix-instantiate --eval --strict . -A tests.kernel-config +# +# make sure to use NON EXISTING kernel settings else they may conflict with +# common-config.nix +{ lib, pkgs }: + +with lib; +with kernel; + +let + lts_kernel = pkgs.linuxPackages.kernel; + + # to see the result once the module transformed the lose structured config + getConfig = structuredConfig: + (lts_kernel.override { + structuredExtraConfig = structuredConfig; + }).configfile.structuredConfig; + + mandatoryVsOptionalConfig = mkMerge [ + { NIXOS_FAKE_USB_DEBUG = yes;} + { NIXOS_FAKE_USB_DEBUG = option yes; } + ]; + + freeformConfig = mkMerge [ + { NIXOS_FAKE_MMC_BLOCK_MINORS = freeform "32"; } # same as default, won't trigger any error + { NIXOS_FAKE_MMC_BLOCK_MINORS = freeform "64"; } # will trigger an error but the message is not great: + ]; + + yesWinsOverNoConfig = mkMerge [ + # default for "NIXOS_TEST_BOOLEAN" is no + { "NIXOS_TEST_BOOLEAN" = yes; } # yes wins over no by default + { "NIXOS_TEST_BOOLEAN" = no; } + ]; + + optionalNoWins = mkMerge [ + { NIXOS_FAKE_USB_DEBUG = option yes;} + { NIXOS_FAKE_USB_DEBUG = yes;} + ]; + + allOptionalRemainOptional = mkMerge [ + { NIXOS_FAKE_USB_DEBUG = option yes;} + { NIXOS_FAKE_USB_DEBUG = option yes;} + ]; + +in +runTests { + testEasy = { + expr = (getConfig { NIXOS_FAKE_USB_DEBUG = yes;}).NIXOS_FAKE_USB_DEBUG; + expected = { tristate = "y"; optional = false; freeform = null; }; + }; + + # mandatory flag should win over optional + testMandatoryCheck = { + expr = (getConfig mandatoryVsOptionalConfig).NIXOS_FAKE_USB_DEBUG.optional; + expected = false; + }; + + testYesWinsOverNo = { + expr = (getConfig yesWinsOverNoConfig)."NIXOS_TEST_BOOLEAN".tristate; + expected = "y"; + }; + + testAllOptionalRemainOptional = { + expr = (getConfig allOptionalRemainOptional)."NIXOS_FAKE_USB_DEBUG".optional; + expected = true; + }; + + # check that freeform options are unique + # Should trigger + # > The option `settings.NIXOS_FAKE_MMC_BLOCK_MINORS.freeform' has conflicting definitions, in `<unknown-file>' and `<unknown-file>' + testTreeform = let + res = builtins.tryEval ( (getConfig freeformConfig).NIXOS_FAKE_MMC_BLOCK_MINORS.freeform); + in { + expr = res.success; + expected = false; + }; + +} diff --git a/nixpkgs/pkgs/test/ld-library-path/default.nix b/nixpkgs/pkgs/test/ld-library-path/default.nix new file mode 100644 index 000000000000..bda3f0be84a6 --- /dev/null +++ b/nixpkgs/pkgs/test/ld-library-path/default.nix @@ -0,0 +1,88 @@ +{ stdenv }: + +# This tests that libraries listed in LD_LIBRARY_PATH take precedence over those listed in RPATH. + +let + # A simple test library: libgreeting.so which exports a single function getGreeting() returning the good old hello greeting. + libgreeting = stdenv.mkDerivation { + name = "libgreeting"; + + code = '' + const char* getGreeting() { return "Hello, world!"; } + ''; + + unpackPhase = '' + echo "$code" > libgreeting.c + ''; + + installPhase = '' + mkdir -p $out/lib + $CC -c -fpic libgreeting.c + $CC -shared libgreeting.o -o $out/lib/libgreeting.so + ''; + }; + + # A variant of libgreeting.so that returns a different message. + libgoodbye = libgreeting.overrideAttrs (_: { + name = "libgoodbye"; + code = '' + const char* getGreeting() { return "Goodbye, world!"; } + ''; + }); + + # A simple consumer of libgreeting.so that just prints the greeting to stdout. + testProgram = stdenv.mkDerivation { + name = "greeting-test"; + + buildInputs = [ libgreeting ]; + + code = '' + #include <stdio.h> + + extern const char* getGreeting(void); + + int main() { + puts(getGreeting()); + } + ''; + + unpackPhase = '' + echo "$code" > greeting-test.c + ''; + + installPhase = '' + mkdir -p $out/bin + $CC -c greeting-test.c + $CC greeting-test.o -lgreeting -o $out/bin/greeting-test + + # Now test the installed binaries right after compiling them. In particular, + # don't do this in installCheckPhase because fixupPhase has been run by then! + ( + export PATH=$out/bin + set -x + + # Verify that our unmodified binary works as expected. + [ "$(greeting-test)" = "Hello, world!" ] + + # And finally, test that a library in LD_LIBRARY_PATH takes precedence over the linked-in library. + [ "$(LD_LIBRARY_PATH=${libgoodbye}/lib greeting-test)" = "Goodbye, world!" ] + ) + ''; + + }; +in stdenv.mkDerivation { + name = "test-LD_LIBRARY_PATH"; + nativeBuildInputs = [ testProgram ]; + + buildCommand = '' + # And for good measure, repeat the tests again from a separate derivation, + # as fixupPhase done by the stdenv can (and has!) affect the result. + + [ "$(greeting-test)" = "Hello, world!" ] + [ "$(LD_LIBRARY_PATH=${libgoodbye}/lib greeting-test)" = "Goodbye, world!" ] + + touch $out + ''; + + meta.platforms = stdenv.lib.platforms.linux; +} diff --git a/nixpkgs/pkgs/test/macos-sierra-shared/default.nix b/nixpkgs/pkgs/test/macos-sierra-shared/default.nix new file mode 100644 index 000000000000..810d5d97829b --- /dev/null +++ b/nixpkgs/pkgs/test/macos-sierra-shared/default.nix @@ -0,0 +1,90 @@ +{ lib, clangStdenv, clang-sierraHack-stdenv, stdenvNoCC }: + +let + makeBigExe = stdenv: prefix: rec { + + count = 320; + + sillyLibs = lib.genList (i: stdenv.mkDerivation rec { + name = "${prefix}-fluff-${toString i}"; + unpackPhase = '' + src=$PWD + cat << 'EOF' > ${name}.c + unsigned int asdf_${toString i}(void) { + return ${toString i}; + } + EOF + ''; + buildPhase = '' + $CC -std=c99 -shared ${name}.c -o lib${name}.dylib -Wl,-install_name,$out/lib/lib${name}.dylib + ''; + installPhase = '' + mkdir -p "$out/lib" + mv lib${name}.dylib "$out/lib" + ''; + meta.platforms = lib.platforms.darwin; + }) count; + + finalExe = stdenv.mkDerivation { + name = "${prefix}-final-asdf"; + unpackPhase = '' + src=$PWD + cat << 'EOF' > main.cxx + + #include <cstdlib> + #include <iostream> + + ${toString (lib.genList (i: "extern \"C\" unsigned int asdf_${toString i}(void); ") count)} + + unsigned int (*funs[])(void) = { + ${toString (lib.genList (i: "asdf_${toString i},") count)} + }; + + int main(int argc, char **argv) { + bool ret; + unsigned int i = 0; + for (auto f : funs) { + if (f() != i++) { + std::cerr << "Failed to get expected response from function #" << i << std::endl; + return EXIT_FAILURE; + } + } + return EXIT_SUCCESS; + } + EOF + ''; + buildPhase = '' + $CXX -std=c++11 main.cxx ${toString (map (x: "-l${x.name}") sillyLibs)} -o ${prefix}-asdf + ''; + buildInputs = sillyLibs; + installPhase = '' + mkdir -p "$out/bin" + mv ${prefix}-asdf "$out/bin" + ''; + meta.platforms = lib.platforms.darwin; + }; + + }; + + good = makeBigExe clang-sierraHack-stdenv "good"; + + bad = makeBigExe clangStdenv "bad"; + +in stdenvNoCC.mkDerivation { + name = "macos-sierra-shared-test"; + buildInputs = [ good.finalExe bad.finalExe ]; + # TODO(@Ericson2314): Be impure or require exact MacOS version of builder? + buildCommand = '' + if bad-asdf &> /dev/null + then echo "WARNING: bad-asdf did not fail, not running on sierra?" >&2 + else echo "bad-asdf should fail on sierra, OK" >&2 + fi + + # Must succeed on all supported MacOS versions + good-asdf + echo "good-asdf should succeed on sierra, OK" + + touch $out + ''; + meta.platforms = lib.platforms.darwin; +} diff --git a/nixpkgs/pkgs/test/mkOption/declare.nix b/nixpkgs/pkgs/test/mkOption/declare.nix new file mode 100644 index 000000000000..9e89a1c096da --- /dev/null +++ b/nixpkgs/pkgs/test/mkOption/declare.nix @@ -0,0 +1,53 @@ +# sets of small configurations: +# Each configuration +rec { + # has 2 arguments pkgs and this. + configA = pkgs: this: { + # Can depends on other configuration + require = configB; + + # Defines new options + optionA = pkgs.lib.mkOption { + # With default values + default = false; + # And merging functions. + merge = pkgs.lib.mergeEnableOption; + }; + + # Add a new definition to other options. + optionB = this.optionA; + }; + + # Can be used for option header. + configB = pkgs: this: { + # Can depends on more than one configuration. + require = [ configC configD ]; + + optionB = pkgs.lib.mkOption { + default = false; + }; + + # Is not obliged to define other options. + }; + + configC = pkgs: this: { + require = [ configA ]; + + optionC = pkgs.lib.mkOption { + default = false; + }; + + # Use the default value if it is not overwritten. + optionA = this.optionC; + }; + + # Can also be used as option configuration only. + # without any arguments (backward compatibility) + configD = { + # Is not forced to specify the require attribute. + + # Is not force to make new options. + optionA = true; + optionD = false; + }; +} diff --git a/nixpkgs/pkgs/test/mkOption/keep.nix b/nixpkgs/pkgs/test/mkOption/keep.nix new file mode 100644 index 000000000000..26fb8c28dd59 --- /dev/null +++ b/nixpkgs/pkgs/test/mkOption/keep.nix @@ -0,0 +1,11 @@ +let + pkgs = import ../../.. {}; + config = import ./declare.nix; +in + with (pkgs.lib); + + finalReferenceOptionSets + filterOptionSets + pkgs + # List of main configurations. + [ config.configB config.configC ] diff --git a/nixpkgs/pkgs/test/mkOption/keep.ref b/nixpkgs/pkgs/test/mkOption/keep.ref new file mode 100644 index 000000000000..a3a051eb48c4 --- /dev/null +++ b/nixpkgs/pkgs/test/mkOption/keep.ref @@ -0,0 +1,57 @@ +<?xml version='1.0' encoding='utf-8'?> +<expr> + <attrs> + <attr name="optionA"> + <list> + <attrs> + <attr name="_type"> + <string value="option" /> + </attr> + <attr name="default"> + <bool value="false" /> + </attr> + <attr name="merge"> + <unevaluated /> + </attr> + <attr name="name"> + <string value="optionA" /> + </attr> + </attrs> + </list> + </attr> + <attr name="optionB"> + <list> + <attrs> + <attr name="_type"> + <string value="option" /> + </attr> + <attr name="default"> + <bool value="false" /> + </attr> + <attr name="name"> + <string value="optionB" /> + </attr> + </attrs> + </list> + </attr> + <attr name="optionC"> + <list> + <attrs> + <attr name="_type"> + <string value="option" /> + </attr> + <attr name="default"> + <bool value="false" /> + </attr> + <attr name="name"> + <string value="optionC" /> + </attr> + </attrs> + </list> + </attr> + <attr name="optionD"> + <attrs> + </attrs> + </attr> + </attrs> +</expr> diff --git a/nixpkgs/pkgs/test/mkOption/merge.nix b/nixpkgs/pkgs/test/mkOption/merge.nix new file mode 100644 index 000000000000..bbf68218aa09 --- /dev/null +++ b/nixpkgs/pkgs/test/mkOption/merge.nix @@ -0,0 +1,15 @@ +let + pkgs = import ../../.. {}; + config = import ./declare.nix; + + # Define the handler of unbound options. + noOption = name: values: + builtins.trace "Attribute named '${name}' does not match any option declaration." values; +in + with (pkgs.lib); + + finalReferenceOptionSets + (mergeOptionSets noOption) + pkgs + # List of main configurations. + [ config.configB config.configC ] diff --git a/nixpkgs/pkgs/test/mkOption/merge.ref b/nixpkgs/pkgs/test/mkOption/merge.ref new file mode 100644 index 000000000000..6956f65dbbcc --- /dev/null +++ b/nixpkgs/pkgs/test/mkOption/merge.ref @@ -0,0 +1,20 @@ +trace: Str("Attribute named 'optionD' does not match any option declaration.",[]) +<?xml version='1.0' encoding='utf-8'?> +<expr> + <attrs> + <attr name="optionA"> + <bool value="true" /> + </attr> + <attr name="optionB"> + <bool value="true" /> + </attr> + <attr name="optionC"> + <bool value="false" /> + </attr> + <attr name="optionD"> + <list> + <bool value="false" /> + </list> + </attr> + </attrs> +</expr> diff --git a/nixpkgs/pkgs/test/mkOption/test.sh b/nixpkgs/pkgs/test/mkOption/test.sh new file mode 100755 index 000000000000..5478846d563f --- /dev/null +++ b/nixpkgs/pkgs/test/mkOption/test.sh @@ -0,0 +1,9 @@ +#! /bin/sh -e + +echo 1>&2 "Test: Merge of option bindings." +nix-instantiate merge.nix --eval-only --strict --xml >& merge.out +diff merge.ref merge.out + +echo 1>&2 "Test: Filter of option declarations." +nix-instantiate keep.nix --eval-only --strict --xml >& keep.out +diff keep.ref keep.out diff --git a/nixpkgs/pkgs/test/nixos-functions/default.nix b/nixpkgs/pkgs/test/nixos-functions/default.nix new file mode 100644 index 000000000000..6a4f3164f929 --- /dev/null +++ b/nixpkgs/pkgs/test/nixos-functions/default.nix @@ -0,0 +1,41 @@ +/* + +This file is a test that makes sure that the `pkgs.nixos` and +`pkgs.nixosTest` functions work. It's far from a perfect test suite, +but better than not checking them at all on hydra. + +To run this test: + + nixpkgs$ nix-build -A tests.nixos-functions + + */ +{ pkgs, lib, stdenv, ... }: + +let + dummyVersioning = { + revision = "test"; + versionSuffix = "test"; + label = "test"; + }; +in lib.optionalAttrs stdenv.hostPlatform.isLinux ( + pkgs.recurseIntoAttrs { + + nixos-test = (pkgs.nixos { + system.nixos = dummyVersioning; + boot.loader.grub.enable = false; + fileSystems."/".device = "/dev/null"; + }).toplevel; + + nixosTest-test = pkgs.nixosTest ({ lib, pkgs, ... }: { + name = "nixosTest-test"; + machine = { pkgs, ... }: { + system.nixos = dummyVersioning; + environment.systemPackages = [ pkgs.hello ]; + }; + testScript = '' + machine.succeed("hello") + ''; + }); + + } +) diff --git a/nixpkgs/pkgs/test/patch-shebangs/default.nix b/nixpkgs/pkgs/test/patch-shebangs/default.nix new file mode 100644 index 000000000000..3e68d96004f2 --- /dev/null +++ b/nixpkgs/pkgs/test/patch-shebangs/default.nix @@ -0,0 +1,26 @@ +{ stdenv, runCommand }: + +let + bad-shebang = stdenv.mkDerivation { + name = "bad-shebang"; + dontUnpack = true; + installPhase = '' + mkdir -p $out/bin + echo "#!/bin/sh" > $out/bin/test + echo "echo -n hello" >> $out/bin/test + chmod +x $out/bin/test + ''; + }; +in runCommand "patch-shebangs-test" { + passthru = { inherit bad-shebang; }; + meta.platforms = stdenv.lib.platforms.all; +} '' + printf "checking whether patchShebangs works properly... ">&2 + if ! grep -q '^#!/bin/sh' ${bad-shebang}/bin/test; then + echo "yes" >&2 + touch $out + else + echo "no" >&2 + exit 1 + fi +'' diff --git a/nixpkgs/pkgs/test/simple/builder.sh b/nixpkgs/pkgs/test/simple/builder.sh new file mode 100644 index 000000000000..65f7e4c11ba1 --- /dev/null +++ b/nixpkgs/pkgs/test/simple/builder.sh @@ -0,0 +1,42 @@ +set -x + +export NIX_DEBUG=1 + +source $stdenv/setup + +export NIX_ENFORCE_PURITY=1 + +mkdir $out +mkdir $out/bin + +cat > hello.c <<EOF +#include <stdio.h> + +int main(int argc, char * * argv) +{ + printf("Hello World!\n"); + return 0; +} +EOF + +#gcc -I/nix/store/foo -I /nix/store/foo -I/usr/lib -I /usr/lib hello.c -o $out/bin/hello +gcc -I`pwd` -L /nix/store/abcd/lib -isystem /usr/lib hello.c -o $out/bin/hello + +$out/bin/hello + +cat > hello2.cc <<EOF +#include <iostream> + +int main(int argc, char * * argv) +{ + std::cout << "Hello World!\n"; + std::cout << VALUE << std::endl; + return 0; +} +EOF + +g++ hello2.cc -o $out/bin/hello2 -DVALUE="1 + 2 * 3" + +$out/bin/hello2 + +ld -v diff --git a/nixpkgs/pkgs/test/stdenv-inputs/bar.c b/nixpkgs/pkgs/test/stdenv-inputs/bar.c new file mode 100644 index 000000000000..2d7299c2d462 --- /dev/null +++ b/nixpkgs/pkgs/test/stdenv-inputs/bar.c @@ -0,0 +1,3 @@ +unsigned int bar(void) { + return 42; +} diff --git a/nixpkgs/pkgs/test/stdenv-inputs/cc-main.c b/nixpkgs/pkgs/test/stdenv-inputs/cc-main.c new file mode 100644 index 000000000000..06f28bc33c69 --- /dev/null +++ b/nixpkgs/pkgs/test/stdenv-inputs/cc-main.c @@ -0,0 +1,7 @@ +#include <stdio.h> + +int main(int argc, char **argv) +{ + fprintf(stderr, "ok\n"); + return 0; +} diff --git a/nixpkgs/pkgs/test/stdenv-inputs/default.nix b/nixpkgs/pkgs/test/stdenv-inputs/default.nix new file mode 100644 index 000000000000..4db10e75086e --- /dev/null +++ b/nixpkgs/pkgs/test/stdenv-inputs/default.nix @@ -0,0 +1,68 @@ +{ stdenv }: + +let + foo = stdenv.mkDerivation { + name = "foo-test"; + + dontUnpack = true; + + installPhase = '' + mkdir -p $out/bin $out/include $out/lib + $CC -o $out/bin/foo ${./cc-main.c} + chmod +x $out/bin/foo + cp ${./foo.c} $out/include/foo.h + $CC -shared \ + ${stdenv.lib.optionalString stdenv.isDarwin "-Wl,-install_name,$out/lib/libfoo.dylib"} \ + -o $out/lib/libfoo${stdenv.hostPlatform.extensions.sharedLibrary} \ + ${./foo.c} + ''; + }; + + bar = stdenv.mkDerivation { + name = "bar-test"; + outputs = [ "out" "dev" ]; + + dontUnpack = true; + + installPhase = '' + mkdir -p $out/bin $dev/include $dev/lib + $CC -o $out/bin/bar ${./cc-main.c} + chmod +x $out/bin/bar + cp ${./bar.c} $dev/include/bar.h + $CC -shared \ + ${stdenv.lib.optionalString stdenv.isDarwin "-Wl,-install_name,$dev/lib/libbar.dylib"} \ + -o $dev/lib/libbar${stdenv.hostPlatform.extensions.sharedLibrary} \ + ${./bar.c} + ''; + }; +in + +stdenv.mkDerivation { + name = "stdenv-inputs-test"; + phases = [ "buildPhase" ]; + + buildInputs = [ foo bar ]; + + buildPhase = '' + env + + printf "checking whether binaries are available... " >&2 + foo && bar + + printf "checking whether compiler can find headers... " >&2 + $CC -o include-check ${./include-main.c} + ./include-check + + printf "checking whether compiler can find headers... " >&2 + $CC -o include-check ${./include-main.c} + ./include-check + + printf "checking whether compiler can find libraries... " >&2 + $CC -lfoo -lbar -o lib-check ${./lib-main.c} + ./lib-check + + touch $out + ''; + + meta.platforms = stdenv.lib.platforms.all; +} diff --git a/nixpkgs/pkgs/test/stdenv-inputs/foo.c b/nixpkgs/pkgs/test/stdenv-inputs/foo.c new file mode 100644 index 000000000000..0253a26d5d2c --- /dev/null +++ b/nixpkgs/pkgs/test/stdenv-inputs/foo.c @@ -0,0 +1,3 @@ +unsigned int foo(void) { + return 42; +} diff --git a/nixpkgs/pkgs/test/stdenv-inputs/include-main.c b/nixpkgs/pkgs/test/stdenv-inputs/include-main.c new file mode 100644 index 000000000000..35e5ee0d90f7 --- /dev/null +++ b/nixpkgs/pkgs/test/stdenv-inputs/include-main.c @@ -0,0 +1,13 @@ +#include <stdio.h> +#include <foo.h> +#include <bar.h> + +int main(int argc, char **argv) +{ + if (foo() != 42) + return 1; + if (bar() != 42) + return 1; + fprintf(stderr, "ok\n"); + return 0; +} diff --git a/nixpkgs/pkgs/test/stdenv-inputs/lib-main.c b/nixpkgs/pkgs/test/stdenv-inputs/lib-main.c new file mode 100644 index 000000000000..c9488fe43e55 --- /dev/null +++ b/nixpkgs/pkgs/test/stdenv-inputs/lib-main.c @@ -0,0 +1,14 @@ +#include <stdio.h> + +extern unsigned int foo(void); +extern unsigned int bar(void); + +int main(int argc, char **argv) +{ + if (foo() != 42) + return 1; + if (bar() != 42) + return 1; + fprintf(stderr, "ok\n"); + return 0; +} |