|
| 1 | +{-# LANGUAGE QuasiQuotes #-} |
| 2 | +{-# OPTIONS_GHC -Wno-missing-import-lists #-} |
| 3 | + |
| 4 | +-- | Copyright : (c) Crown Copyright GCHQ |
| 5 | +module Bootstrap.Data.Bootstrappable.BuildNixSpec (spec) where |
| 6 | + |
| 7 | +import Bootstrap.Data.Bootstrappable (Bootstrappable (bootstrapContent)) |
| 8 | +import Bootstrap.Data.Bootstrappable.BuildNix (buildNixFor) |
| 9 | +import Bootstrap.Data.ProjectName (ProjectName, mkProjectName) |
| 10 | +import Bootstrap.Data.ProjectType |
| 11 | + ( ArtefactId (ArtefactId), |
| 12 | + InstallLombok (InstallLombok), |
| 13 | + InstallMinishift (InstallMinishift), |
| 14 | + JavaOptions (JavaOptions), |
| 15 | + ProjectType (Go, Java), |
| 16 | + SetUpGoBuild (SetUpGoBuild), |
| 17 | + SetUpJavaBuild (SetUpJavaBuild), |
| 18 | + ) |
| 19 | +import qualified Relude.Unsafe as Unsafe |
| 20 | +import Test.Hspec (Spec, describe, it) |
| 21 | +import Test.Hspec.Expectations.Pretty (shouldBe) |
| 22 | +import Test.Util.RunConfig (rcDefault, rcWithFlakes) |
| 23 | +import Text.RawString.QQ (r) |
| 24 | + |
| 25 | +spec :: Spec |
| 26 | +spec = describe "build.nix rendering" do |
| 27 | + it "renders nothing for a non-flake project" do |
| 28 | + buildNixFor rcDefault projectName (Go $ SetUpGoBuild True) |
| 29 | + `shouldBe` Nothing |
| 30 | + it "renders correctly for a Go project" do |
| 31 | + case buildNixFor rcWithFlakes projectName (Go $ SetUpGoBuild True) of |
| 32 | + Just buildNix -> |
| 33 | + bootstrapContent buildNix |
| 34 | + >>= ( `shouldBe` |
| 35 | + Right |
| 36 | + [r|{nixpkgs}: |
| 37 | +nixpkgs.buildGoModule { |
| 38 | + pname = "test-project"; |
| 39 | + version = "0.1.0"; |
| 40 | + src = ./.; |
| 41 | + vendorSha256 = null; |
| 42 | + # Swap out the line above for the one below once you start adding dependencies. |
| 43 | + # After your dependencies change, builds will fail until you update the hash below. |
| 44 | + # When the build fails, it will tell you what the expected hash is. |
| 45 | + # vendorSha256 = "sha256-00000000000000000000000000000000000000000000"; |
| 46 | +} |
| 47 | +|] |
| 48 | + ) |
| 49 | + Nothing -> fail "Gave nothing for a project which should've had a build.nix generated." |
| 50 | + |
| 51 | + it "renders correctly for a Java project" do |
| 52 | + case buildNixFor |
| 53 | + rcWithFlakes |
| 54 | + projectName |
| 55 | + ( Java $ |
| 56 | + JavaOptions |
| 57 | + (InstallMinishift True) |
| 58 | + (InstallLombok True) |
| 59 | + (SetUpJavaBuild $ ArtefactId "testId") |
| 60 | + ) of |
| 61 | + Just buildNix -> |
| 62 | + bootstrapContent buildNix |
| 63 | + >>= ( `shouldBe` |
| 64 | + Right |
| 65 | + [r|{nixpkgs}: let |
| 66 | + projectName = "test-project"; |
| 67 | + artefactId = "testId"; |
| 68 | + repository = nixpkgs.stdenv.mkDerivation { |
| 69 | + name = "${projectName}-repository"; |
| 70 | + buildInputs = [nixpkgs.maven]; |
| 71 | + src = ./.; |
| 72 | + buildPhase = "mvn package -Dmaven.repo.local=$out"; |
| 73 | + # keep only *.{pom,jar,sha1,nbm} and delete all ephemeral files with lastModified timestamps inside |
| 74 | + installPhase = '' |
| 75 | + find $out -type f \ |
| 76 | + -name \*.lastUpdated -or \ |
| 77 | + -name resolver-status.properties -or \ |
| 78 | + -name _remote.repositories \ |
| 79 | + -delete |
| 80 | + ''; |
| 81 | + dontFixup = true; |
| 82 | + outputHashAlgo = "sha256"; |
| 83 | + outputHashMode = "recursive"; |
| 84 | + # This hash will need updating when changing your dependencies; the correct |
| 85 | + # hash will be displayed when the build fails if so. |
| 86 | + outputHash = "sha256-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="; |
| 87 | + }; |
| 88 | + builtJar = nixpkgs.stdenv.mkDerivation rec { |
| 89 | + pname = projectName; |
| 90 | + version = "0.0.1-SNAPSHOT"; |
| 91 | + src = ./.; |
| 92 | + buildInputs = [nixpkgs.maven]; |
| 93 | + buildPhase = '' |
| 94 | + echo "Using repository ${repository}" |
| 95 | + mvn --offline -Dmaven.repo.local=${repository} package; |
| 96 | + ''; |
| 97 | + installPhase = '' |
| 98 | + install -Dm644 target/${artefactId}-${version}.jar $out/${projectName} |
| 99 | + ''; |
| 100 | + }; |
| 101 | + fromImage = nixpkgs.dockerTools.pullImage { |
| 102 | + imageName = "eclipse-temurin"; |
| 103 | + imageDigest = "sha256:4cc7dfdfb7837f35c3820bcfbc5f666521364e2198960322848ab7d3e2ca3e88"; |
| 104 | + finalImageName = "eclipse-temurin"; |
| 105 | + finalImageTag = "17.0.4.1_1-jre"; |
| 106 | + sha256 = "sha256-OIib6PQJc1xX+Xu2xtaFEo/jZtxypkg8Y9RFMcJf39w="; |
| 107 | + }; |
| 108 | +in |
| 109 | + nixpkgs.dockerTools.buildLayeredImage { |
| 110 | + inherit fromImage; |
| 111 | + name = projectName; |
| 112 | + enableFakechroot = true; |
| 113 | + fakeRootCommands = '' |
| 114 | + cp ${builtJar}/${projectName} /${projectName} |
| 115 | + chown root:root /${projectName} |
| 116 | + chmod 774 /${projectName} |
| 117 | + ''; |
| 118 | + config = { |
| 119 | + Entrypoint = [ |
| 120 | + "/bin/sh" |
| 121 | + "-c" |
| 122 | + "java $JAVA_OPTS -jar /${projectName}" |
| 123 | + ]; |
| 124 | + }; |
| 125 | + } |
| 126 | +|] |
| 127 | + ) |
| 128 | + Nothing -> fail "Gave nothing for a project which should've had a build.nix generated." |
| 129 | + |
| 130 | +projectName :: ProjectName |
| 131 | +projectName = Unsafe.fromJust (mkProjectName "test-project") |
0 commit comments