Repository navigation
Expand file tree
/
Copy pathtest.sh
More file actions
executable file
·135 lines (124 loc) · 5.12 KB
/
Copy pathtest.sh
File metadata and controls
executable file
·135 lines (124 loc) · 5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/bin/bash
set -e
usage() {
echo "This script uses Docker to run tests in a standardized environment."
echo "This is most useful on macOS, where golden tests (ran natively) will "
echo "often fail due to differences in text rendering."
echo
echo "You can use this script by replacing any \`flutter test\` command with"
echo "this script, and passing the same arguments to it."
echo "e.g. instead of running:"
echo " flutter test --update-goldens"
echo "you can run:"
echo " $0 --update-goldens"
echo
echo "There are also some additional arguments you can pass to this script:"
echo " --help: Show this help message"
echo " --clean: Delete the docker image and container"
echo " --update: Update the container's Flutter and pub cache from the host."
}
if [[ "$1" == "--help" ]]; then
usage
exit 0
fi
DOCKERFILE_PATH=".github/docker/Dockerfile"
IMAGE_NAME="ricochlime_test_env"
CONTAINER_NAME="ricochlime_test_container"
# Check if docker is installed
if ! command -v docker &> /dev/null; then
echo "Docker is not installed. Please install Docker to use this script."
echo "https://docs.docker.com/get-docker/"
exit 1
fi
# Check if docker engine is running
if ! docker version >/dev/null 2>&1; then
echo "Docker Engine does not seem to be running: run it first and retry!"
exit 1
fi
# Check that we're in the project root
if [[ ! -f "privacy_policy.md" ]]; then
echo "Error: Please run this script from the project root."
exit 1
fi
if [ ! -d "$FLUTTER_ROOT" ]; then
echo "Error: Env FLUTTER_ROOT not set."
exit 1
fi
if [ ! -d "${PUB_CACHE:=$HOME/.pub-cache}" ]; then
echo "Error: Env PUB_CACHE not set."
exit 1
fi
# Remove docker image and container by running `./test.sh --clean`
if [[ "$1" == "--clean" ]]; then
echo 'Stopping docker container'
docker stop "$CONTAINER_NAME" || true
echo 'Removing docker container'
docker rm -f "$CONTAINER_NAME" || true
echo 'Removing docker image'
docker rmi -f "$IMAGE_NAME" || true
echo 'Removing cached files'
rm -rf .github/docker/mounts
echo 'Done'
exit 0
fi
# Update flutter and pub cache by running `./test.sh --update`
copy_flutter_from_host() {
mkdir -p .github/docker/mounts
[ -d .github/docker/mounts/flutter ] || cp -r "$FLUTTER_ROOT" .github/docker/mounts/flutter
[ -d .github/docker/mounts/.pub-cache ] || cp -r "$PUB_CACHE" .github/docker/mounts/.pub-cache
[ -d .github/docker/mounts/.dart_tool ] || cp -r .dart_tool .github/docker/mounts/.dart_tool
[ -f .github/docker/mounts/pubspec.lock ] || cp pubspec.lock .github/docker/mounts/pubspec.lock
}
if [[ "$1" == "--update" ]]; then
rm -rf .github/docker/mounts/{flutter,.pub-cache,.dart_tool,pubspec.lock}
copy_flutter_from_host
exit 0
fi
# Get current directory
if [[ "$OSTYPE" == "msys"* || "$OSTYPE" == "cygwin"* ]]; then
APP_PATH="$(pwd -W)" # Windows-style path on Windows
else
APP_PATH="$(pwd)"
fi
echo "Using application path: $APP_PATH"
# Build image if it doesn't exist
if ! docker images --format '{{.Repository}}' | grep -w "$IMAGE_NAME" > /dev/null; then
echo "Building Docker image '$IMAGE_NAME' from $DOCKERFILE_PATH. This will take a few minutes but only needs to be done once."
docker build -t "$IMAGE_NAME" \
-f "$DOCKERFILE_PATH" \
--network host \
--build-arg APP_PATH="$APP_PATH" \
"$(dirname $DOCKERFILE_PATH)"
fi
# Make sure mounts exist
mkdir -p .github/docker/mounts
copy_flutter_from_host
mkdir -p .github/docker/mounts/build
mkdir -p .github/docker/mounts/linux-flutter-ephemeral
mkdir -p .github/docker/mounts/macos-flutter-ephemeral
mkdir -p .github/docker/mounts/windows-flutter-ephemeral
touch .github/docker/mounts/.flutter-plugins-dependencies
# Start or create container from image
if docker ps -a --format '{{.Names}}' | grep -w "$CONTAINER_NAME" > /dev/null; then
echo "Starting existing container $CONTAINER_NAME"
docker start "$CONTAINER_NAME"
else
echo "Creating new container $CONTAINER_NAME. This will only be done once."
# Run container
docker run -dit --name "$CONTAINER_NAME" \
-u nonroot \
-v "$APP_PATH":"$APP_PATH" \
-v "$APP_PATH/.github/docker/mounts/flutter":"/home/nonroot/flutter" \
-v "$APP_PATH/.github/docker/mounts/.pub-cache":"/home/nonroot/.pub-cache" \
-v "$APP_PATH/.github/docker/mounts/.dart_tool":"$APP_PATH/.dart_tool" \
-v "$APP_PATH/.github/docker/mounts/build":"$APP_PATH/build" \
-v "$APP_PATH/.github/docker/mounts/.flutter-plugins-dependencies":"$APP_PATH/.flutter-plugins-dependencies" \
-v "$APP_PATH/.github/docker/mounts/pubspec.lock":"$APP_PATH/pubspec.lock" \
-v "$APP_PATH/.github/docker/mounts/linux-flutter-ephemeral":"$APP_PATH/linux/flutter/ephemeral" \
-v "$APP_PATH/.github/docker/mounts/macos-flutter-ephemeral":"$APP_PATH/macos/Flutter/ephemeral" \
-v "$APP_PATH/.github/docker/mounts/windows-flutter-ephemeral":"$APP_PATH/windows/flutter/ephemeral" \
"$IMAGE_NAME"
docker exec -it "$CONTAINER_NAME" flutter pub get
fi
docker exec -it "$CONTAINER_NAME" flutter test "$@"
exit $?