Skip to content

Commit

Permalink
Add test of the toolchains mechanism (ocaml#10798)
Browse files Browse the repository at this point in the history
Signed-off-by: Stephen Sherratt <stephen@sherra.tt>
  • Loading branch information
gridbugs authored and anmonteiro committed Nov 17, 2024
1 parent 68ab5fa commit a91e23e
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions test/blackbox-tests/test-cases/pkg/toolchain-installation.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
Test the installation of toolchains package by building and installing
a mock compiler package using dune's toolchain mechanism.

$ . ./helpers.sh
$ make_lockdir

$ mkdir fake-compiler

A configure script for the fake compiler that writes the prefix path
to a file so the fake compiler can be installed to the specified
location, testing that dune respects the "prefix" variable for
toolchain packages.
$ cat > fake-compiler/configure << 'EOF'
> #!/bin/sh
> PREFIX=$1
> echo $PREFIX > prefix.txt
> EOF
$ chmod a+x fake-compiler/configure

A shell script that will be installed to the toolchain bin directory
and masquerade as the compiler.
$ mkdir -p fake-compiler/target/bin
$ cat > fake-compiler/target/bin/ocamlc << EOF
> #!/bin/sh
> echo "Hello from fake ocamlc!"
> EOF
$ chmod a+x fake-compiler/target/bin/ocamlc

The makefile for the fake compiler copies the fake ocamlc script into
the appropriate location, respecting the DESTDIR variable.
$ cat > fake-compiler/Makefile << 'EOF'
> prefix := $(shell cat prefix.txt)
> target := $(DESTDIR)$(prefix)
> install:
> @mkdir -p $(target)
> @cp -r target/* $(target)
> EOF

Lockfile for the fake compiler package:
$ cat > dune.lock/ocaml-base-compiler.pkg << EOF
> (version 1)
>
> (build
> (run ./configure %{prefix}))
>
> (install
> (run %{make} install))
>
> (source
> (copy $PWD/fake-compiler))
> EOF

$ cat > dune-project << EOF
> (lang dune 3.16)
>
> (package
> (name foo)
> (depends ocaml-base-compiler))
> EOF

$ cat > dune << EOF
> (executable
> (public_name foo))
> EOF

$ cat > foo.ml << EOF
> print_endline "Hello, World!"
> EOF

Toolchain directory names contain a hash of some of the fields from
the package's lockfile. This lockfile includes the expanded $PWD
variable which includes the test sandbox path which is different each
time the tests runs. Strip the hash out of the toolchain directory
name so the output is consistent across test runs.
$ remove_hash() {
> sed 's/\(ocaml-base-compiler.1-\)[^/]*/\1HASH/'
> }

Attempt to build the project. This will fail due to the fake compiler
but the fake compiler will end up installed as a toolchain package.
$ XDG_CACHE_HOME=$PWD/fake-cache DUNE_CONFIG__TOOLCHAINS_ENABLED=true dune build 2>&1 | remove_hash
Error: Failed to parse the output of
'$TESTCASE_ROOT/fake-cache/dune/toolchains/ocaml-base-compiler.1-HASH/target/bin/ocamlc
-config':
Unrecognized line: "Hello from fake ocamlc!"

Enumerate the contents of the fake toolchains directory:
$ find fake-cache | sort | remove_hash
fake-cache
fake-cache/dune
fake-cache/dune/toolchains
fake-cache/dune/toolchains/ocaml-base-compiler.1-HASH
fake-cache/dune/toolchains/ocaml-base-compiler.1-HASH/target
fake-cache/dune/toolchains/ocaml-base-compiler.1-HASH/target/bin
fake-cache/dune/toolchains/ocaml-base-compiler.1-HASH/target/bin/ocamlc

0 comments on commit a91e23e

Please sign in to comment.