Skip to content

Commit 4117135

Browse files
committed
refactor(test): add millw into test resources
1 parent bee918d commit 4117135

File tree

2 files changed

+228
-28
lines changed

2 files changed

+228
-28
lines changed
Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
#!/usr/bin/env sh
2+
3+
# This is a wrapper script, that automatically download mill from GitHub release pages
4+
# You can give the required mill version with --mill-version parameter
5+
# If no version is given, it falls back to the value of DEFAULT_MILL_VERSION
6+
#
7+
# Project page: https://github.com/lefou/millw
8+
# Script Version: 0.4.2
9+
#
10+
# If you want to improve this script, please also contribute your changes back!
11+
#
12+
# Licensed under the Apache License, Version 2.0
13+
14+
15+
DEFAULT_MILL_VERSION=0.10.0
16+
17+
set -e
18+
19+
MILL_REPO_URL="https://github.com/com-lihaoyi/mill"
20+
21+
if [ -z "${CURL_CMD}" ] ; then
22+
CURL_CMD=curl
23+
fi
24+
25+
# Explicit commandline argument takes precedence over all other methods
26+
if [ "$1" = "--mill-version" ] ; then
27+
shift
28+
if [ "x$1" != "x" ] ; then
29+
MILL_VERSION="$1"
30+
shift
31+
else
32+
echo "You specified --mill-version without a version." 1>&2
33+
echo "Please provide a version that matches one provided on" 1>&2
34+
echo "${MILL_REPO_URL}/releases" 1>&2
35+
false
36+
fi
37+
fi
38+
39+
# Please note, that if a MILL_VERSION is already set in the environment,
40+
# We reuse it's value and skip searching for a value.
41+
42+
# If not already set, read .mill-version file
43+
if [ -z "${MILL_VERSION}" ] ; then
44+
if [ -f ".mill-version" ] ; then
45+
MILL_VERSION="$(head -n 1 .mill-version 2> /dev/null)"
46+
fi
47+
fi
48+
49+
if [ -n "${XDG_CACHE_HOME}" ] ; then
50+
MILL_DOWNLOAD_PATH="${XDG_CACHE_HOME}/mill/download"
51+
else
52+
MILL_DOWNLOAD_PATH="${HOME}/.cache/mill/download"
53+
fi
54+
55+
# If not already set, try to fetch newest from Github
56+
if [ -z "${MILL_VERSION}" ] ; then
57+
# TODO: try to load latest version from release page
58+
echo "No mill version specified." 1>&2
59+
echo "You should provide a version via '.mill-version' file or --mill-version option." 1>&2
60+
61+
mkdir -p "${MILL_DOWNLOAD_PATH}"
62+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest" 2>/dev/null || (
63+
# we might be on OSX or BSD which don't have -d option for touch
64+
# but probably a -A [-][[hh]mm]SS
65+
touch "${MILL_DOWNLOAD_PATH}/.expire_latest"; touch -A -010000 "${MILL_DOWNLOAD_PATH}/.expire_latest"
66+
) || (
67+
# in case we still failed, we retry the first touch command with the intention
68+
# to show the (previously suppressed) error message
69+
LANG=C touch -d '1 hour ago' "${MILL_DOWNLOAD_PATH}/.expire_latest"
70+
)
71+
72+
# POSIX shell variant of bash's -nt operator, see https://unix.stackexchange.com/a/449744/6993
73+
# if [ "${MILL_DOWNLOAD_PATH}/.latest" -nt "${MILL_DOWNLOAD_PATH}/.expire_latest" ] ; then
74+
if [ -n "$(find -L "${MILL_DOWNLOAD_PATH}/.latest" -prune -newer "${MILL_DOWNLOAD_PATH}/.expire_latest")" ]; then
75+
# we know a current latest version
76+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
77+
fi
78+
79+
if [ -z "${MILL_VERSION}" ] ; then
80+
# we don't know a current latest version
81+
echo "Retrieving latest mill version ..." 1>&2
82+
LANG=C ${CURL_CMD} -s -i -f -I ${MILL_REPO_URL}/releases/latest 2> /dev/null | grep --ignore-case Location: | sed s'/^.*tag\///' | tr -d '\r\n' > "${MILL_DOWNLOAD_PATH}/.latest"
83+
MILL_VERSION=$(head -n 1 "${MILL_DOWNLOAD_PATH}"/.latest 2> /dev/null)
84+
fi
85+
86+
if [ -z "${MILL_VERSION}" ] ; then
87+
# Last resort
88+
MILL_VERSION="${DEFAULT_MILL_VERSION}"
89+
echo "Falling back to hardcoded mill version ${MILL_VERSION}" 1>&2
90+
else
91+
echo "Using mill version ${MILL_VERSION}" 1>&2
92+
fi
93+
fi
94+
95+
MILL="${MILL_DOWNLOAD_PATH}/${MILL_VERSION}"
96+
97+
try_to_use_system_mill() {
98+
MILL_IN_PATH="$(command -v mill || true)"
99+
100+
if [ -z "${MILL_IN_PATH}" ]; then
101+
return
102+
fi
103+
104+
UNIVERSAL_SCRIPT_MAGIC="@ 2>/dev/null # 2>nul & echo off & goto BOF"
105+
106+
if ! head -c 128 "${MILL_IN_PATH}" | grep -qF "${UNIVERSAL_SCRIPT_MAGIC}"; then
107+
if [ -n "${MILLW_VERBOSE}" ]; then
108+
echo "Could not determine mill version of ${MILL_IN_PATH}, as it does not start with the universal script magic2" 1>&2
109+
fi
110+
return
111+
fi
112+
113+
# Roughly the size of the universal script.
114+
MILL_VERSION_SEARCH_RANGE="2403"
115+
MILL_IN_PATH_VERSION=$(head -c "${MILL_VERSION_SEARCH_RANGE}" "${MILL_IN_PATH}" |\
116+
sed -n 's/^.*-DMILL_VERSION=\([^\s]*\) .*$/\1/p' |\
117+
head -n 1)
118+
119+
if [ -z "${MILL_IN_PATH_VERSION}" ]; then
120+
echo "Could not determine mill version, even though ${MILL_IN_PATH} has the universal script magic" 1>&2
121+
return
122+
fi
123+
124+
if [ "${MILL_IN_PATH_VERSION}" = "${MILL_VERSION}" ]; then
125+
MILL="${MILL_IN_PATH}"
126+
fi
127+
}
128+
try_to_use_system_mill
129+
130+
# If not already downloaded, download it
131+
if [ ! -s "${MILL}" ] ; then
132+
133+
# support old non-XDG download dir
134+
MILL_OLD_DOWNLOAD_PATH="${HOME}/.mill/download"
135+
OLD_MILL="${MILL_OLD_DOWNLOAD_PATH}/${MILL_VERSION}"
136+
if [ -x "${OLD_MILL}" ] ; then
137+
MILL="${OLD_MILL}"
138+
else
139+
VERSION_PREFIX="$(echo $MILL_VERSION | cut -b -4)"
140+
case $VERSION_PREFIX in
141+
0.0. | 0.1. | 0.2. | 0.3. | 0.4. )
142+
DOWNLOAD_SUFFIX=""
143+
;;
144+
*)
145+
DOWNLOAD_SUFFIX="-assembly"
146+
;;
147+
esac
148+
unset VERSION_PREFIX
149+
150+
DOWNLOAD_FILE=$(mktemp mill.XXXXXX)
151+
# TODO: handle command not found
152+
echo "Downloading mill ${MILL_VERSION} from ${MILL_REPO_URL}/releases ..." 1>&2
153+
MILL_VERSION_TAG=$(echo $MILL_VERSION | sed -E 's/([^-]+)(-M[0-9]+)?(-.*)?/\1\2/')
154+
${CURL_CMD} -f -L -o "${DOWNLOAD_FILE}" "${MILL_REPO_URL}/releases/download/${MILL_VERSION_TAG}/${MILL_VERSION}${DOWNLOAD_SUFFIX}"
155+
chmod +x "${DOWNLOAD_FILE}"
156+
mkdir -p "${MILL_DOWNLOAD_PATH}"
157+
mv "${DOWNLOAD_FILE}" "${MILL}"
158+
159+
unset DOWNLOAD_FILE
160+
unset DOWNLOAD_SUFFIX
161+
fi
162+
fi
163+
164+
unset MILL_DOWNLOAD_PATH
165+
unset MILL_OLD_DOWNLOAD_PATH
166+
unset OLD_MILL
167+
unset MILL_VERSION
168+
unset MILL_VERSION_TAG
169+
unset MILL_REPO_URL
170+
171+
exec "${MILL}" "$@"
Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,61 @@
11
package tests
22

3+
import java.nio.file.Files
4+
import java.nio.file.Paths
5+
import java.nio.file.StandardCopyOption
6+
import java.nio.file.attribute.PosixFilePermission
7+
8+
import scala.jdk.CollectionConverters._
9+
310
class MillBuildToolSuite extends BaseBuildToolSuite {
4-
checkBuild(
5-
s"minimal",
6-
s"""|/.mill-version
7-
|0.10.5
8-
|/build.sc
9-
|import mill._, scalalib._
10-
|object minimal extends ScalaModule {
11-
| def scalaVersion = "2.13.8"
12-
| object test extends Tests with TestModule.Munit {
13-
| def ivyDeps = Agg(ivy"org.scalameta::munit:1.0.0-M6")
14-
| }
15-
|}
16-
|/minimal/src/Main.scala
17-
|package minimal
18-
|object Main extends App
19-
|/minimal/test/src/MainSuite.scala
20-
|package minimal
21-
|class MainSpec extends munit.FunSuite {
22-
| test("numbers") {
23-
| assertEquals(1, 1)
24-
| }
25-
|}
26-
|""".stripMargin,
27-
expectedSemanticdbFiles = 2,
28-
expectedPackages =
29-
"""|maven:munit:munit:1.0.0-M6
30-
|""".stripMargin
31-
)
11+
12+
def setupMill() = {
13+
val mill = workingDirectory.resolve("mill")
14+
val resource = getClass().getResource("/mill")
15+
val in = Paths.get(resource.toURI)
16+
17+
Files.createDirectories(mill.getParent)
18+
Files.copy(in, mill, StandardCopyOption.REPLACE_EXISTING)
19+
Files.setPosixFilePermissions(
20+
mill,
21+
Set(
22+
PosixFilePermission.OWNER_READ,
23+
PosixFilePermission.OWNER_WRITE,
24+
PosixFilePermission.OWNER_EXECUTE
25+
).asJava
26+
)
27+
List("./mill", "--version")
28+
}
29+
30+
List("0.10.0").foreach { version =>
31+
checkBuild(
32+
s"minimal",
33+
s"""|/.mill-version
34+
|${version}
35+
|/build.sc
36+
|import mill._, scalalib._
37+
|object minimal extends ScalaModule {
38+
| def scalaVersion = "2.13.8"
39+
| object test extends Tests with TestModule.Munit {
40+
| def ivyDeps = Agg(ivy"org.scalameta::munit:1.0.0-M6")
41+
| }
42+
|}
43+
|/minimal/src/Main.scala
44+
|package minimal
45+
|object Main extends App
46+
|/minimal/test/src/MainSuite.scala
47+
|package minimal
48+
|class MainSpec extends munit.FunSuite {
49+
| test("numbers") {
50+
| assertEquals(1, 1)
51+
| }
52+
|}
53+
|""".stripMargin,
54+
expectedSemanticdbFiles = 2,
55+
expectedPackages =
56+
"""|maven:munit:munit:1.0.0-M6
57+
|""".stripMargin,
58+
initCommand = setupMill()
59+
)
60+
}
3261
}

0 commit comments

Comments
 (0)