From 6198ef85f2df48d0e63e60a60019ee598e1b5d5d Mon Sep 17 00:00:00 2001 From: Alexandre Archambault Date: Sat, 22 Sep 2018 18:32:40 +0200 Subject: [PATCH] Run test repo from a more practical script (#901) --- appveyor.yml | 19 +++++--- .../credentials-from-file/build.sbt | 2 +- .../sbt-coursier/credentials-sbt/build.sbt | 9 +++- .../sbt-coursier/credentials/build.sbt | 7 ++- scripts/launch-test-repo.sh | 20 --------- scripts/start-it-auth-server.ps1 | 2 +- scripts/start-it-no-listing-server.ps1 | 3 -- scripts/travis.sh | 17 ++----- scripts/with-test-repo.sh | 45 +++++++++++++++++++ .../coursier/test/DirectoryListingTests.scala | 6 +-- .../test/HttpAuthenticationTests.scala | 43 +++++------------- 11 files changed, 89 insertions(+), 84 deletions(-) delete mode 100755 scripts/launch-test-repo.sh delete mode 100644 scripts/start-it-no-listing-server.ps1 create mode 100755 scripts/with-test-repo.sh diff --git a/appveyor.yml b/appveyor.yml index 18f8990d4..47be3595e 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -15,17 +15,24 @@ install: - cmd: SET PATH=C:\sbt\sbt\bin;%JAVA_HOME%\bin;%PATH% - cmd: SET SBT_OPTS=-Xmx4g -Xss2m - git submodule update --init --recursive +environment: + TEST_REPOSITORY_HOST: localhost + TEST_REPOSITORY_PORT: 8080 + TEST_REPOSITORY_USER: user + TEST_REPOSITORY_PASSWORD: pass + TEST_REPOSITORY: http://localhost:8080 build_script: + - ps: Start-Job -filepath .\scripts\start-it-auth-server.ps1 -ArgumentList $pwd, $env:TEST_REPOSITORY_HOST, $env:TEST_REPOSITORY_PORT, $env:TEST_REPOSITORY_USER, $env:TEST_REPOSITORY_PASSWORD - sbt scala212 coreJVM/publishLocal cacheJVM/publishLocal extra/publishLocal scalazJVM/publishLocal cli/publishLocal - sbt scala211 compile coreJVM/publishLocal - sbt scala210 compile test_script: - - ps: Start-Job -filepath .\scripts\start-it-auth-server.ps1 -ArgumentList $pwd - - ps: Start-Sleep -s 15 # wait for the first server to have downloaded its dependencies - - ps: Start-Job -filepath .\scripts\start-it-no-listing-server.ps1 -ArgumentList $pwd - - sbt scala212 testsJVM/test testsJVM/it:test # Would node be around for testsJS/test? - - sbt scala211 testsJVM/test testsJVM/it:test - - sbt scala210 testsJVM/test testsJVM/it:test + - sbt scala212 testsJVM/test + - sbt scala211 testsJVM/test + - sbt scala210 testsJVM/test + - sbt scala212 testsJVM/it:test + - sbt scala211 testsJVM/it:test + - sbt scala210 testsJVM/it:test - sbt scala212 "sbt-coursier/scripted sbt-coursier/simple" "sbt-shading/scripted sbt-shading/*" # for sbt 1.0 - sbt scala210 "sbt-coursier/scripted sbt-coursier/*" "sbt-coursier/scripted sbt-coursier-0.13/*" "sbt-shading/scripted sbt-shading/*" "sbt-shading/scripted sbt-shading-0.13/*" # for sbt 0.13 branches: diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/credentials-from-file/build.sbt b/sbt-coursier/src/sbt-test/sbt-coursier/credentials-from-file/build.sbt index 49e4b5f44..7ffe593d5 100644 --- a/sbt-coursier/src/sbt-test/sbt-coursier/credentials-from-file/build.sbt +++ b/sbt-coursier/src/sbt-test/sbt-coursier/credentials-from-file/build.sbt @@ -1,6 +1,6 @@ scalaVersion := "2.11.8" -resolvers += "authenticated" at "http://localhost:8080" +resolvers += "authenticated" at sys.env("TEST_REPOSITORY") coursierCredentials += "authenticated" -> coursier.Credentials(file("credentials")) diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/credentials-sbt/build.sbt b/sbt-coursier/src/sbt-test/sbt-coursier/credentials-sbt/build.sbt index bc325e22f..38eade154 100644 --- a/sbt-coursier/src/sbt-test/sbt-coursier/credentials-sbt/build.sbt +++ b/sbt-coursier/src/sbt-test/sbt-coursier/credentials-sbt/build.sbt @@ -1,8 +1,13 @@ scalaVersion := "2.11.8" -resolvers += "authenticated" at "http://localhost:8080" +resolvers += "authenticated" at sys.env("TEST_REPOSITORY") coursierUseSbtCredentials := true -credentials += Credentials("", "localhost", "user", "pass") +credentials += Credentials( + "", + sys.env("TEST_REPOSITORY_HOST"), + sys.env("TEST_REPOSITORY_USER"), + sys.env("TEST_REPOSITORY_PASSWORD") +) libraryDependencies += "com.abc" % "test" % "0.1" diff --git a/sbt-coursier/src/sbt-test/sbt-coursier/credentials/build.sbt b/sbt-coursier/src/sbt-test/sbt-coursier/credentials/build.sbt index eca8505e1..36d2ead16 100644 --- a/sbt-coursier/src/sbt-test/sbt-coursier/credentials/build.sbt +++ b/sbt-coursier/src/sbt-test/sbt-coursier/credentials/build.sbt @@ -1,7 +1,10 @@ scalaVersion := "2.11.8" -resolvers += "authenticated" at "http://localhost:8080" +resolvers += "authenticated" at sys.env("TEST_REPOSITORY") -coursierCredentials += "authenticated" -> coursier.Credentials("user", "pass") +coursierCredentials += "authenticated" -> coursier.Credentials( + sys.env("TEST_REPOSITORY_USER"), + sys.env("TEST_REPOSITORY_PASSWORD") +) libraryDependencies += "com.abc" % "test" % "0.1" diff --git a/scripts/launch-test-repo.sh b/scripts/launch-test-repo.sh deleted file mode 100755 index c4ec96172..000000000 --- a/scripts/launch-test-repo.sh +++ /dev/null @@ -1,20 +0,0 @@ -#!/usr/bin/env bash -set -e - -VERSION="1.0.0" - -cd "$(dirname "$0")/.." - -# synchronously fill cache so that two runs of this script don't try to download -# a same file at the same time (and one of them fail because of locks) -./coursier fetch \ - "io.get-coursier:http-server_2.12:$VERSION" \ - -r https://dl.bintray.com/scalaz/releases - -./coursier launch \ - "io.get-coursier:http-server_2.12:$VERSION" \ - -- \ - -d tests/jvm/src/test/resources/test-repo/http/abc.com \ - -u user -P pass -r realm \ - -v \ - "$@" & diff --git a/scripts/start-it-auth-server.ps1 b/scripts/start-it-auth-server.ps1 index b81d38b92..8b24fc465 100644 --- a/scripts/start-it-auth-server.ps1 +++ b/scripts/start-it-auth-server.ps1 @@ -1,3 +1,3 @@ # see https://stackoverflow.com/questions/2224350/powershell-start-job-working-directory/2246542#2246542 Set-Location $args[0] -& java -jar -noverify coursier launch io.get-coursier:http-server_2.12:1.0.0 -- -d tests/jvm/src/test/resources/test-repo/http/abc.com -u user -P pass -r realm --port 8080 --list-pages -v +& java -jar -noverify coursier launch io.get-coursier:http-server_2.12:1.0.0 -- -d tests/jvm/src/test/resources/test-repo/http/abc.com -u $args[3] -P $args[4] -r realm --host $args[1] --port $args[2] -v diff --git a/scripts/start-it-no-listing-server.ps1 b/scripts/start-it-no-listing-server.ps1 deleted file mode 100644 index ee1fc2b26..000000000 --- a/scripts/start-it-no-listing-server.ps1 +++ /dev/null @@ -1,3 +0,0 @@ -# see https://stackoverflow.com/questions/2224350/powershell-start-job-working-directory/2246542#2246542 -Set-Location $args[0] -& java -jar -noverify coursier launch io.get-coursier:http-server_2.12:1.0.0 -- -d tests/jvm/src/test/resources/test-repo/http/abc.com -u user -P pass -r realm --port 8081 -v diff --git a/scripts/travis.sh b/scripts/travis.sh index 4157fe137..a2c993013 100755 --- a/scripts/travis.sh +++ b/scripts/travis.sh @@ -13,19 +13,9 @@ downloadInstallSbtExtras() { chmod +x bin/sbt } -launchTestRepo() { - ./scripts/launch-test-repo.sh "$@" -} - integrationTestsRequirements() { # Required for ~/.ivy2/local repo tests sbt scala211 coreJVM/publishLocal scala212 cli/publishLocal - - # Required for HTTP authentication tests - launchTestRepo --port 8080 --list-pages - - # Required for missing directory listing tests (no --list-pages) - launchTestRepo --port 8081 } isScalaJs() { @@ -43,10 +33,11 @@ sbtShading() { runSbtCoursierTests() { addPgpKeys if [ "$SCALA_VERSION" = "2.10" ]; then - sbt scalaFromEnv "sbt-coursier/scripted sbt-coursier/*" "sbt-coursier/scripted sbt-coursier-0.13/*" + CMDS=("sbt-coursier/scripted sbt-coursier/*" "sbt-coursier/scripted sbt-coursier-0.13/*") else - sbt scalaFromEnv "sbt-coursier/scripted sbt-coursier/simple" # full scripted suite currently taking too long on Travis CI... + CMDS=("sbt-coursier/scripted sbt-coursier/simple") # full scripted suite currently taking too long on Travis CI... fi + ./scripts/with-test-repo.sh sbt scalaFromEnv "${CMD[@]}" sbt scalaFromEnv sbt-pgp-coursier/scripted } @@ -76,7 +67,7 @@ runJvmTests() { IT="jvm/it:test" fi - sbt scalaFromEnv jvm/test $IT + ./scripts/with-test-repo.sh sbt scalaFromEnv jvm/test $IT } validateReadme() { diff --git a/scripts/with-test-repo.sh b/scripts/with-test-repo.sh new file mode 100755 index 000000000..47ccc5136 --- /dev/null +++ b/scripts/with-test-repo.sh @@ -0,0 +1,45 @@ +#!/usr/bin/env bash +set -e + +cd "$(dirname "$0")/.." + +SERVER_PID="" + +cleanup() { + if [ ! -z "$SERVER_PID" ]; then + echo "Terminating background HTTP server" + kill -15 "$SERVER_PID" + while kill -0 "$SERVER_PID" >/dev/null 2>&1; do + echo "Server still running" + sleep 1 + kill -15 "$SERVER_PID" >/dev/null 2>&1 || true + done + echo "Server terminated" + fi +} + +trap cleanup EXIT INT TERM + +export TEST_REPOSITORY_HOST="localhost" +export TEST_REPOSITORY_PORT="8080" +export TEST_REPOSITORY_USER="user" +export TEST_REPOSITORY_PASSWORD="pass" + +export TEST_REPOSITORY="http://$TEST_REPOSITORY_HOST:$TEST_REPOSITORY_PORT" + +# see https://unix.stackexchange.com/questions/90244/bash-run-command-in-background-and-capture-pid +runServerBg() { + java -jar ./coursier launch \ + "io.get-coursier:http-server_2.12:1.0.0" \ + -- \ + -d tests/jvm/src/test/resources/test-repo/http/abc.com \ + -u "$TEST_REPOSITORY_USER" -P "$TEST_REPOSITORY_PASSWORD" -r realm \ + -v \ + --host "$TEST_REPOSITORY_HOST" \ + --port "$TEST_REPOSITORY_PORT" & + SERVER_PID="$!" +} + +runServerBg + +"$@" diff --git a/tests/jvm/src/it/scala/coursier/test/DirectoryListingTests.scala b/tests/jvm/src/it/scala/coursier/test/DirectoryListingTests.scala index 6a62b444b..03f91b652 100644 --- a/tests/jvm/src/it/scala/coursier/test/DirectoryListingTests.scala +++ b/tests/jvm/src/it/scala/coursier/test/DirectoryListingTests.scala @@ -6,11 +6,11 @@ import utest._ object DirectoryListingTests extends TestSuite { - val user = "user" - val password = "pass" + val user = sys.env("TEST_REPOSITORY_USER") + val password = sys.env("TEST_REPOSITORY_PASSWORD") val repo = MavenRepository( - "http://localhost:8080", + sys.env.getOrElse("TEST_REPOSITORY", sys.error("TEST_REPOSITORY not set")), authentication = Some(Authentication(user, password)) ) diff --git a/tests/jvm/src/it/scala/coursier/test/HttpAuthenticationTests.scala b/tests/jvm/src/it/scala/coursier/test/HttpAuthenticationTests.scala index 952fa3b73..fb18ea752 100644 --- a/tests/jvm/src/it/scala/coursier/test/HttpAuthenticationTests.scala +++ b/tests/jvm/src/it/scala/coursier/test/HttpAuthenticationTests.scala @@ -9,39 +9,22 @@ object HttpAuthenticationTests extends TestSuite { val tests = Tests { 'httpAuthentication - { - // requires an authenticated HTTP server to be running on localhost:8080 with user 'user' - // and password 'pass' - val address = "localhost:8080" - val user = "user" - val password = "pass" - - def printErrorMessage() = - Console.err.println( - Console.RED + - s"HTTP authentication tests require a running HTTP server on $address, requiring " + - s"basic authentication with user '$user' and password '$password', serving the right " + - "files.\n" + Console.RESET + - "Run one from the coursier sources with\n" + - " ./coursier launch -r https://dl.bintray.com/scalaz/releases " + - "io.get-coursier:simple-web-server_2.11:1.0.0-M12 -- " + - "-d tests/jvm/src/test/resources/test-repo/http/abc.com -u user -P pass -r realm -v" - ) + val testRepo = sys.env.getOrElse("TEST_REPOSITORY", sys.error("TEST_REPOSITORY not set")) + val user = sys.env.getOrElse("TEST_REPOSITORY_USER", sys.error("TEST_REPOSITORY_USER not set")) + val password = sys.env.getOrElse("TEST_REPOSITORY_PASSWORD", sys.error("TEST_REPOSITORY_PASSWORD not set")) * - { // no authentication -> should fail val failed = try { CacheFetchTests.check( - MavenRepository( - s"http://$address" - ) + MavenRepository(testRepo) ) - printErrorMessage() false } catch { - case e: Throwable => + case _: Throwable => true } @@ -51,18 +34,12 @@ object HttpAuthenticationTests extends TestSuite { * - { // with authentication -> should work - try { - CacheFetchTests.check( - MavenRepository( - s"http://$address", - authentication = Some(Authentication(user, password)) - ) + CacheFetchTests.check( + MavenRepository( + testRepo, + authentication = Some(Authentication(user, password)) ) - } catch { - case e: Throwable => - printErrorMessage() - throw e - } + ) } } }