e2e-installs.sh 7.56 KB
Newer Older
1
#!/bin/bash
Christopher Chedeau's avatar
Christopher Chedeau committed
2
# Copyright (c) 2015-present, Facebook, Inc.
Joe Haddad's avatar
Joe Haddad committed
3
4
5
#
# This source code is licensed under the MIT license found in the
# LICENSE file in the root directory of this source tree.
Christopher Chedeau's avatar
Christopher Chedeau committed
6

7
8
9
10
11
# ******************************************************************************
# This is an end-to-end test intended to run on CI.
# You can also run it locally but it's slow.
# ******************************************************************************

Dan Abramov's avatar
Dan Abramov committed
12
# Start in tasks/ even if run from root directory
Christopher Chedeau's avatar
Christopher Chedeau committed
13
14
cd "$(dirname "$0")"

15
16
17
# CLI and app temporary locations
# http://unix.stackexchange.com/a/84980
temp_app_path=`mktemp -d 2>/dev/null || mktemp -d -t 'temp_app_path'`
18
19
20
custom_registry_url=http://localhost:4873
original_npm_registry_url=`npm get registry`
original_yarn_registry_url=`yarn config get registry`
21

22
23
function cleanup {
  echo 'Cleaning up.'
24
  cd "$root_path"
25
  rm -rf "$temp_app_path"
26
27
  npm set registry "$original_npm_registry_url"
  yarn config set registry "$original_yarn_registry_url"
28
29
}

Dan Abramov's avatar
Dan Abramov committed
30
# Error messages are redirected to stderr
31
function handle_error {
Dan Abramov's avatar
Dan Abramov committed
32
  echo "$(basename $0): ERROR! An error was encountered executing line $1." 1>&2;
Dan Abramov's avatar
Dan Abramov committed
33
  cleanup
34
  echo 'Exiting with error.' 1>&2;
35
36
37
38
39
  exit 1
}

function handle_exit {
  cleanup
40
  echo 'Exiting without error.' 1>&2;
41
42
43
  exit
}

44
45
46
47
48
49
50
# Check for the existence of one or more files.
function exists {
  for f in $*; do
    test -e "$f"
  done
}

51
52
53
54
55
56
57
58
59
60
61
# Check for accidental dependencies in package.json
function checkDependencies {
  if ! awk '/"dependencies": {/{y=1;next}/},/{y=0; next}y' package.json | \
  grep -v -q -E '^\s*"react(-dom|-scripts)?"'; then
   echo "Dependencies are correct"
  else
   echo "There are extraneous dependencies in package.json"
   exit 1
  fi
}

62
63
64
65
66
# Exit the script with a helpful error message when any error is encountered
trap 'set +x; handle_error $LINENO $BASH_COMMAND' ERR

# Cleanup before exit on any termination signal
trap 'set +x; handle_exit' SIGQUIT SIGTERM SIGINT SIGKILL SIGHUP
Christopher Chedeau's avatar
Christopher Chedeau committed
67
68
69
70

# Echo every command being executed
set -x

Dan Abramov's avatar
Dan Abramov committed
71
# Go to root
Christopher Chedeau's avatar
Christopher Chedeau committed
72
cd ..
Dan Abramov's avatar
Dan Abramov committed
73
root_path=$PWD
74

75
76
77
78
79
80
if hash npm 2>/dev/null
then
  npm i -g npm@latest
  npm cache clean || npm cache verify
fi

81
# Bootstrap monorepo
82
yarn
83

Dan Abramov's avatar
Dan Abramov committed
84
# ******************************************************************************
85
# First, publish the monorepo.
Dan Abramov's avatar
Dan Abramov committed
86
87
# ******************************************************************************

88
89
90
91
92
93
94
# Start local registry
tmp_registry_log=`mktemp`
nohup npx verdaccio@2.7.2 &>$tmp_registry_log &
# Wait for `verdaccio` to boot
grep -q 'http address' <(tail -f $tmp_registry_log)

# Set registry to local registry
95
96
npm set registry "$custom_registry_url"
yarn config set registry "$custom_registry_url"
97
98

# Login so we can publish packages
99
npx npm-cli-login@0.0.10 -u user -p password -e user@example.com -r "$custom_registry_url" --quotes
Christopher Chedeau's avatar
Christopher Chedeau committed
100

101
# Publish the monorepo
102
103
git clean -df
./tasks/publish.sh --yes --force-publish=* --skip-git --cd-version=prerelease --exact --npm-tag=latest
Christopher Chedeau's avatar
Christopher Chedeau committed
104

105
# ******************************************************************************
Dan Abramov's avatar
Dan Abramov committed
106
# Test --scripts-version with a version number
107
108
# ******************************************************************************

109
cd "$temp_app_path"
110
npx create-react-app --scripts-version=1.0.17 test-app-version-number
111
112
113
cd test-app-version-number

# Check corresponding scripts version is installed.
114
exists node_modules/react-scripts
115
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
116
checkDependencies
117

118
119
120
121
122
# ******************************************************************************
# Test --use-npm flag
# ******************************************************************************

cd "$temp_app_path"
123
npx create-react-app --use-npm --scripts-version=1.0.17 test-use-npm-flag
124
125
126
127
128
cd test-use-npm-flag

# Check corresponding scripts version is installed.
exists node_modules/react-scripts
[ ! -e "yarn.lock" ] && echo "yarn.lock correctly does not exist"
129
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
130
131
checkDependencies

132
# ******************************************************************************
Dan Abramov's avatar
Dan Abramov committed
133
# Test --scripts-version with a tarball url
134
135
# ******************************************************************************

136
cd "$temp_app_path"
137
npx create-react-app --scripts-version=https://registry.npmjs.org/react-scripts/-/react-scripts-1.0.17.tgz test-app-tarball-url
138
139
140
cd test-app-tarball-url

# Check corresponding scripts version is installed.
141
exists node_modules/react-scripts
142
grep '"version": "1.0.17"' node_modules/react-scripts/package.json
143
checkDependencies
144
145

# ******************************************************************************
Dan Abramov's avatar
Dan Abramov committed
146
# Test --scripts-version with a custom fork of react-scripts
147
148
# ******************************************************************************

149
cd "$temp_app_path"
150
npx create-react-app --scripts-version=react-scripts-fork test-app-fork
151
152
153
cd test-app-fork

# Check corresponding scripts version is installed.
154
exists node_modules/react-scripts-fork
155

156
157
158
159
# ******************************************************************************
# Test project folder is deleted on failing package installation
# ******************************************************************************

160
cd "$temp_app_path"
161
# we will install a non-existing package to simulate a failed installataion.
162
npx create-react-app --scripts-version=`date +%s` test-app-should-not-exist || true
Dan Abramov's avatar
Dan Abramov committed
163
164
165
# confirm that the project files were deleted
test ! -e test-app-should-not-exist/package.json
test ! -d test-app-should-not-exist/node_modules
166
167
168
169
170

# ******************************************************************************
# Test project folder is not deleted when creating app over existing folder
# ******************************************************************************

171
cd "$temp_app_path"
172
173
174
mkdir test-app-should-remain
echo '## Hello' > ./test-app-should-remain/README.md
# we will install a non-existing package to simulate a failed installataion.
175
npx create-react-app --scripts-version=`date +%s` test-app-should-remain || true
176
177
# confirm the file exist
test -e test-app-should-remain/README.md
Dan Abramov's avatar
Dan Abramov committed
178
179
# confirm only README.md and error log are the only files in the directory
if [ "$(ls -1 ./test-app-should-remain | wc -l | tr -d '[:space:]')" != "2" ]; then
180
181
182
  false
fi

183
184
185
186
187
188
# ******************************************************************************
# Test --scripts-version with a scoped fork tgz of react-scripts
# ******************************************************************************

cd $temp_app_path
curl "https://registry.npmjs.org/@enoah_netzach/react-scripts/-/react-scripts-0.9.0.tgz" -o enoah-scripts-0.9.0.tgz
189
npx create-react-app --scripts-version=$temp_app_path/enoah-scripts-0.9.0.tgz test-app-scoped-fork-tgz
190
191
192
193
194
cd test-app-scoped-fork-tgz

# Check corresponding scripts version is installed.
exists node_modules/@enoah_netzach/react-scripts

195
196
197
198
# ******************************************************************************
# Test nested folder path as the project name
# ******************************************************************************

199
# Testing a path that exists
200
cd "$temp_app_path"
201
202
203
mkdir test-app-nested-paths-t1
cd test-app-nested-paths-t1
mkdir -p test-app-nested-paths-t1/aa/bb/cc/dd
204
npx create-react-app test-app-nested-paths-t1/aa/bb/cc/dd
205
cd test-app-nested-paths-t1/aa/bb/cc/dd
206
yarn start --smoke-test
207

208
# Testing a path that does not exist
209
cd "$temp_app_path"
210
npx create-react-app test-app-nested-paths-t2/aa/bb/cc/dd
211
cd test-app-nested-paths-t2/aa/bb/cc/dd
212
yarn start --smoke-test
213

214
# Testing a path that is half exists
215
cd "$temp_app_path"
216
mkdir -p test-app-nested-paths-t3/aa
217
npx create-react-app test-app-nested-paths-t3/aa/bb/cc/dd
218
cd test-app-nested-paths-t3/aa/bb/cc/dd
219
yarn start --smoke-test
220

Christopher Chedeau's avatar
Christopher Chedeau committed
221
# Cleanup
222
cleanup