Skip to content

Commit

Permalink
Simplify addition of mock-repo in a lot of tests (#11185)
Browse files Browse the repository at this point in the history
A lot of tests just do the very basic addition of the mock-repo into the
workspace with the repo overridden sometimes, so this applies the helper
function wherever that's easily possible.

Signed-off-by: Marek Kubica <[email protected]>
  • Loading branch information
Leonidas-from-XIV authored Dec 9, 2024
1 parent bca9e65 commit b544d27
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 173 deletions.
16 changes: 3 additions & 13 deletions test/blackbox-tests/test-cases/pkg/commit-hash-references.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,7 @@ What happens if a branch has the same format as a ref?

Use this ref in a project

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "git+file://$PWD/mock-opam-repository#$AMBIGUOUS_REF"))
> (context
> (default
> (name default)))
> EOF
$ add_mock_repo_if_needed "git+file://$PWD/mock-opam-repository#$AMBIGUOUS_REF"

Depend on foo from the repo

Expand All @@ -41,5 +31,5 @@ Depend on foo from the repo

Which foo will we get?

$ dune pkg lock 2>&1 | head -1 | sed "s/$AMBIGUOUS_REF/AMBIGUOUS_REF/g"
revision "AMBIGUOUS_REF" not found in
$ dune pkg lock 2>&1 | head -1 | sed "s/$AMBIGUOUS_REF/\$AMBIGUOUS_REF/g"
revision "$AMBIGUOUS_REF" not found in
12 changes: 1 addition & 11 deletions test/blackbox-tests/test-cases/pkg/git-repo.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,7 @@ We want to make sure our OPAM-repository in git support works well.
We'll set up a project that uses (only this) this repository, so doesn't use
:standard:

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "git+file://$PWD/mock-opam-repository"))
> (context
> (default
> (name default)))
> EOF
$ add_mock_repo_if_needed "git+file://$PWD/mock-opam-repository"

We depend on the foo package

Expand Down
7 changes: 5 additions & 2 deletions test/blackbox-tests/test-cases/pkg/helpers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ mkpkg() {
}

add_mock_repo_if_needed() {
# default, but can be overridden, e.g. if git is required
repo="${1:-file://$(pwd)/mock-opam-repository}"

if [ ! -e dune-workspace ]
then
cat >dune-workspace <<EOF
Expand All @@ -52,7 +55,7 @@ add_mock_repo_if_needed() {
(repositories mock))
(repository
(name mock)
(url "file://$(pwd)/mock-opam-repository"))
(url "${repo}"))
EOF
else
if ! grep '(name mock)' > /dev/null dune-workspace
Expand All @@ -61,7 +64,7 @@ EOF
cat >>dune-workspace <<EOF
(repository
(name mock)
(url "file://$(pwd)/mock-opam-repository"))
(url "${repo}"))
EOF

# reference the repo
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
Test the error cases for invalid opam repositories

$ . ../helpers.sh

$ cat >dune-project <<EOF
> (lang dune 3.8)
> (package
> (name lockfile_generation_test))
> EOF
> cat >dune-workspace <<EOF
> (lang dune 3.8)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/directory-that-does-not-exist"))
> EOF
$ add_mock_repo_if_needed "file://$(pwd)/directory-that-does-not-exist"

$ lock() {
> out="$(dune pkg lock 2>&1)"
Expand All @@ -32,14 +27,8 @@ Test the error cases for invalid opam repositories
[1]

$ touch empty
$ cat >dune-workspace <<EOF
> (lang dune 3.8)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/empty"))
> EOF
$ rm dune-workspace
$ add_mock_repo_if_needed "file://$(pwd)/empty"
$ lock
File "dune-workspace", line 6, characters X-X:
6 | (url ..))
Expand All @@ -48,14 +37,8 @@ Test the error cases for invalid opam repositories
is not a directory
[1]

$ cat >dune-workspace <<EOF
> (lang dune 3.8)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/no-packages-dir"))
> EOF
$ rm dune-workspace
$ add_mock_repo_if_needed "file://$(pwd)/no-packages-dir"
$ lock
File "dune-workspace", line 6, characters X-X:
6 | (url ..))
Expand Down
9 changes: 1 addition & 8 deletions test/blackbox-tests/test-cases/pkg/invalid-version.t
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,7 @@ In this case we could also hint at the correct syntax for dune-project files.
> (name invalid)
> (depends foo.1.2.3))
> EOF
$ cat >dune-workspace <<EOF
> (lang dune 3.13)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/mock-opam-repository"))
> EOF
$ add_mock_repo_if_needed

$ dune pkg lock 2>&1
File "dune-project", line 4, characters 10-19:
Expand Down
9 changes: 1 addition & 8 deletions test/blackbox-tests/test-cases/pkg/lockdir-tampering.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ Define some local packages.
> (package (name foo) (depends a (b (>= 0.0.2))))
> (package (name bar) (depends foo c))
> EOF
$ cat >dune-workspace <<EOF
> (lang dune 3.11)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/mock-opam-repository"))
> EOF
$ add_mock_repo_if_needed

Without a lockdir this command prints a hint but exits successfully.
$ dune pkg validate-lockdir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,7 @@ Generate a `dune-project` file.
> "bar" {>= "0.2"}
> ]
> EOF
> cat >dune-workspace <<EOF
> (lang dune 3.8)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/mock-opam-repository"))
> EOF
$ add_mock_repo_if_needed

Run the solver and generate a lock directory.

Expand Down
81 changes: 15 additions & 66 deletions test/blackbox-tests/test-cases/pkg/opam-repository-download.t
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,7 @@ Make a mock repo tarball that will get used by dune to download the package
> (name baz)
> (depends bar))
> EOF
$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository"))
> EOF
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository"

$ dune pkg lock
Solution for dune.lock:
Expand All @@ -51,15 +44,8 @@ Make sure lock.dune contains the repo hash:
Now try it with an a path. Given it is not a git URL, it can't be reproduced on
other systems and thus shouldn't be included.

$ rm -r dune.lock
$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "file://$(pwd)/mock-opam-repository"))
> EOF
$ rm -r dune.lock dune-workspace
$ add_mock_repo_if_needed "file://$(pwd)/mock-opam-repository"
$ dune pkg lock
Solution for dune.lock:
- bar.0.0.1
Expand All @@ -80,15 +66,8 @@ in the repo and make sure it locks the older version.
$ NEW_REPO_HASH=$(git rev-parse HEAD)
$ cd ..

$ rm -r dune.lock
$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository#${REPO_HASH}"))
> EOF
$ rm -r dune.lock dune-workspace
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository#${REPO_HASH}"
$ dune pkg lock
Solution for dune.lock:
- bar.0.0.1
Expand All @@ -98,14 +77,8 @@ in the repo and make sure it locks the older version.
If we specify no branch however, it should be using the latest commit in the
repository and thus the new foo package.

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name foo)
> (url "git+file://$(pwd)/mock-opam-repository"))
> (lock_dir
> (repositories foo))
> EOF
$ rm dune-workspace
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository"
$ dune pkg lock
Solution for dune.lock:
- bar.0.0.1
Expand All @@ -126,14 +99,8 @@ A new package is released in the repo:
Since we have a working cached copy we get the old version of `bar` if we opt
out of the auto update.

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository#${NEW_REPO_HASH}"))
> (lock_dir
> (repositories mock))
> EOF
$ rm dune-workspace
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository#${NEW_REPO_HASH}"

To be safe it doesn't access the repo, we make sure to move the mock-repo away

Expand All @@ -150,14 +117,8 @@ So now the test should work as it can't access the repo:
But it will also get the new version of bar if we attempt to lock again (having
restored the repo to where it was before)

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository#${NEWEST_REPO_HASH}"))
> (lock_dir
> (repositories mock))
> EOF
$ rm -r dune-workspace
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository#${NEWEST_REPO_HASH}"
$ mv elsewhere mock-opam-repository
$ rm -r dune.lock
$ dune pkg lock
Expand All @@ -179,14 +140,8 @@ sure that the default branch differs from `bar-2`).
$ git switch --quiet -
$ cd ..

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository#bar-2"))
> (lock_dir
> (repositories mock))
> EOF
$ rm dune-workspace
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository#bar-2"

Locking that branch should work and pick `bar.2.0.0`:

Expand All @@ -211,14 +166,8 @@ of the main branch.

The repo should be using the `1.0` tag, as we don't want `bar.3.0.0`.

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository#1.0"))
> (lock_dir
> (repositories mock))
> EOF
$ rm dune-workspace
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository#1.0"

So we should get `bar.1.0.0` when locking.

Expand Down
14 changes: 1 addition & 13 deletions test/blackbox-tests/test-cases/pkg/rev-store-lock-linux.t
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,7 @@ Thus we first create a repo:
$ git add -A
$ git commit --quiet -m "Initial commit"
$ cd ..

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository"))
> (lock_dir
> (repositories mock))
> (context
> (default
> (name default)
> (lock_dir dune.lock)))
> EOF
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository"

We set the project up to depend on `foo`

Expand Down
13 changes: 1 addition & 12 deletions test/blackbox-tests/test-cases/pkg/rev-store-lock.t
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@ To start with we create a repository in with a `foo` package.

We set this repository as sole source for opam repositories.

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (lock_dir
> (repositories mock))
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository"))
> (context
> (default
> (name default)
> (lock_dir dune.lock)))
> EOF
$ add_mock_repo_if_needed

We set the project up to depend on `foo`

Expand Down
9 changes: 1 addition & 8 deletions test/blackbox-tests/test-cases/pkg/submodules.t
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,7 @@ In our mock repository, we make sure to add the package submodule as

We'll use the mock repository as source and depend on `bar`:

$ cat > dune-workspace <<EOF
> (lang dune 3.10)
> (repository
> (name mock)
> (url "git+file://$(pwd)/mock-opam-repository"))
> (lock_dir
> (repositories mock))
> EOF
$ add_mock_repo_if_needed "git+file://$(pwd)/mock-opam-repository"
$ cat > dune-project <<EOF
> (lang dune 3.10)
> (package
Expand Down

0 comments on commit b544d27

Please sign in to comment.