Makefile 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. DOCKER_COMPOSE_FLAGS ?= -f docker-compose.yml
  2. export RETRIES ?= $(if $(CI),3,0)
  3. export BUILD_NUMBER ?= local
  4. ifeq ($(BRANCH_NAME),)
  5. export BRANCH_NAME := $(shell git rev-parse --abbrev-ref HEAD)
  6. endif
  7. export BRANCH_NAME_TAG_SAFE := $(shell echo "$(BRANCH_NAME)" | sed 's/\//\-\-/g' | cut -c1-96)
  8. .PHONY: print-branch-tag-safe
  9. print-branch-tag-safe:
  10. @echo $(BRANCH_NAME_TAG_SAFE)
  11. ifeq ($(COMMIT_SHA),)
  12. export COMMIT_SHA := $(shell git rev-parse HEAD)
  13. endif
  14. export PROJECT_NAME = web
  15. BUILD_DIR_NAME := web
  16. PWD := $(shell pwd)
  17. ifeq ($(MONOREPO),)
  18. export MONOREPO := $(shell cd ../../ && pwd)
  19. endif
  20. export OVERLEAF_CONFIG ?= /overleaf/services/web/test/acceptance/config/settings.test.saas.js
  21. export BASE_CONFIG ?= ${OVERLEAF_CONFIG}
  22. CFG_SAAS=/overleaf/services/web/test/acceptance/config/settings.test.saas.js
  23. CFG_SERVER_CE=/overleaf/services/web/test/acceptance/config/settings.test.server-ce.js
  24. CFG_SERVER_PRO=/overleaf/services/web/test/acceptance/config/settings.test.server-pro.js
  25. export MOCHA_GREP ?=
  26. DOCKER_COMPOSE := docker compose ${DOCKER_COMPOSE_FLAGS}
  27. # Common flags for docker compose run.
  28. # --rm Tear down test runner container
  29. # --build Make sure that we use the latest base image
  30. # --quiet-build Hide build output
  31. ifeq ($(CI),true)
  32. DC_RUN_FLAGS := --rm
  33. else
  34. DC_RUN_FLAGS := --rm --build --quiet-build
  35. endif
  36. MODULE_DIRS := $(shell find modules -mindepth 1 -maxdepth 1 -type d -not -name '.git' )
  37. MODULE_MAKEFILES := $(MODULE_DIRS:=/Makefile)
  38. MODULE_NAME=$(shell basename $(MODULE))
  39. create_module_Makefiles: $(MODULE_MAKEFILES)
  40. $(MODULE_MAKEFILES): Makefile.module
  41. cp Makefile.module $@ || diff Makefile.module $@
  42. MODULE_APP_SRC_DIRS := $(shell find modules -mindepth 3 -maxdepth 3 -type d -path '*/app/src')
  43. MODULE_TSCONFIGS := $(MODULE_APP_SRC_DIRS:=/tsconfig.json)
  44. create_module_tsconfigs: $(MODULE_TSCONFIGS)
  45. $(MODULE_TSCONFIGS): tsconfig.module.json
  46. cp tsconfig.module.json $@ || diff tsconfig.module.json $@
  47. #
  48. # Clean
  49. #
  50. SHARD_PROJECT_NAMES = \
  51. unit_test_$(BUILD_DIR_NAME) \
  52. unit_test_all_$(BUILD_DIR_NAME) \
  53. unit_test_esm_$(BUILD_DIR_NAME) \
  54. unit_test_esm_watch_$(BUILD_DIR_NAME) \
  55. unit_test_mocha_$(BUILD_DIR_NAME) \
  56. unit_test_parallel_$(BUILD_DIR_NAME) \
  57. unit_test_parallel_make_$(BUILD_DIR_NAME) \
  58. acceptance_test_saas_$(BUILD_DIR_NAME) \
  59. acceptance_test_server_ce_$(BUILD_DIR_NAME) \
  60. acceptance_test_server_pro_$(BUILD_DIR_NAME) \
  61. acceptance_test_modules_merged_saas_$(BUILD_DIR_NAME) \
  62. acceptance_test_modules_merged_saas_1_$(BUILD_DIR_NAME) \
  63. acceptance_test_modules_merged_saas_2_$(BUILD_DIR_NAME) \
  64. acceptance_test_modules_merged_saas_3_$(BUILD_DIR_NAME) \
  65. acceptance_test_modules_merged_saas_4_$(BUILD_DIR_NAME) \
  66. acceptance_test_modules_merged_saas_5_$(BUILD_DIR_NAME) \
  67. acceptance_test_modules_merged_server_ce_$(BUILD_DIR_NAME) \
  68. acceptance_test_modules_merged_server_pro_$(BUILD_DIR_NAME) \
  69. frontend_test_$(BUILD_DIR_NAME) \
  70. test_frontend_ct_$(BUILD_DIR_NAME) \
  71. test_frontend_ct_core_other_$(BUILD_DIR_NAME) \
  72. test_frontend_ct_core_features_$(BUILD_DIR_NAME) \
  73. test_frontend_ct_modules_$(BUILD_DIR_NAME) \
  74. test_frontend_ct_editor_other_$(BUILD_DIR_NAME) \
  75. test_frontend_ct_editor_visual_$(BUILD_DIR_NAME) \
  76. tar_$(BUILD_DIR_NAME) \
  77. CLEAN_SHARDS=$(addprefix clean/,$(SHARD_PROJECT_NAMES))
  78. clean: $(CLEAN_SHARDS)
  79. -docker run --rm --volume /dev/shm:/dev/shm --user root $(IMAGE_CI) rm -rf /dev/shm/overleaf
  80. -docker rmi --force $(IMAGE_CI) $(IMAGE_CI)-dev $(IMAGE_CI)-pug $(IMAGE_CI)-webpack $(IMAGE_REPO_FINAL)
  81. -docker compose down --remove-orphans --rmi local --timeout 0 --volumes
  82. -git clean -dfX data/
  83. $(CLEAN_SHARDS): clean/%:
  84. -COMPOSE_PROJECT_NAME=$* docker compose down --remove-orphans --rmi local --timeout 0 --volumes
  85. clean_ci:
  86. $(DOCKER_COMPOSE) down -v -t 0
  87. docker container list | grep 'days ago' | cut -d ' ' -f 1 - | xargs -r docker container stop
  88. docker image prune -af --filter "until=48h"
  89. docker network prune -f
  90. #
  91. # Tests
  92. #
  93. test: test_unit test_acceptance test_frontend test_writefull
  94. test_module: test_unit_module test_acceptance_module
  95. #
  96. # Unit tests
  97. #
  98. test_unit: test_unit_all
  99. test_unit_all: export COMPOSE_PROJECT_NAME=unit_test_all_$(BUILD_DIR_NAME)
  100. test_unit_all: mongo_migrations_for_tests
  101. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit yarn run test:unit:all
  102. test_unit_all_silent: export COMPOSE_PROJECT_NAME=unit_test_all_$(BUILD_DIR_NAME)
  103. test_unit_all_silent: mongo_migrations_for_tests
  104. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit yarn run test:unit:all:silent
  105. test_unit_app: export COMPOSE_PROJECT_NAME=unit_test_$(BUILD_DIR_NAME)
  106. test_unit_app: mongo_migrations_for_tests
  107. $(DOCKER_COMPOSE) run --name unit_test_$(BUILD_DIR_NAME) --rm test_unit
  108. test_unit_parallel: export JUNIT_ROOT_SUITE_NAME = ESM unit tests - parallel
  109. test_unit_parallel: export COMPOSE_PROJECT_NAME=unit_test_parallel_$(BUILD_DIR_NAME)
  110. test_unit_parallel: mongo_migrations_for_tests
  111. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit yarn run test:unit:parallel
  112. test_unit_sequential: export JUNIT_ROOT_SUITE_NAME = ESM unit tests - sequential
  113. test_unit_sequential: export COMPOSE_PROJECT_NAME=unit_test_sequential_$(BUILD_DIR_NAME)
  114. test_unit_sequential: mongo_migrations_for_tests
  115. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit yarn run test:unit:sequential
  116. test_unit_watch: export COMPOSE_PROJECT_NAME=unit_test_watch_$(BUILD_DIR_NAME)
  117. test_unit_watch: mongo_migrations_for_tests
  118. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit yarn run test:unit:watch
  119. TEST_SUITES = $(sort $(filter-out \
  120. $(wildcard test/unit/src/helpers/*), \
  121. $(wildcard test/unit/src/*/*)))
  122. MOCHA_CMD_LINE = \
  123. mocha \
  124. --exit \
  125. --file test/unit/bootstrap.js \
  126. --grep="${MOCHA_GREP}" \
  127. --reporter spec \
  128. --timeout 25000 \
  129. .PHONY: $(TEST_SUITES)
  130. $(TEST_SUITES):
  131. $(MOCHA_CMD_LINE) $@
  132. J ?= 1
  133. test_unit_app_parallel_gnu_make: $(TEST_SUITES)
  134. test_unit_app_parallel_gnu_make_docker: export COMPOSE_PROJECT_NAME = \
  135. unit_test_parallel_make_$(BUILD_DIR_NAME)
  136. test_unit_app_parallel_gnu_make_docker: mongo_migrations_for_tests
  137. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit \
  138. make test_unit_app_parallel_gnu_make --output-sync -j $(J)
  139. TEST_UNIT_MODULES = $(MODULE_DIRS:=/test_unit)
  140. $(TEST_UNIT_MODULES): %/test_unit: %/Makefile
  141. test_unit_modules: mongo_migrations_for_tests $(TEST_UNIT_MODULES)
  142. test_unit_module: mongo_migrations_for_tests
  143. $(MAKE) modules/$(MODULE_NAME)/test_unit
  144. mongo_migrations_for_tests:
  145. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) --workdir /overleaf/tools/migrations test_unit yarn run migrations migrate -t saas
  146. #
  147. # Frontend tests
  148. #
  149. test_frontend_jsdom: export JUNIT_ROOT_SUITE_NAME = JSDOM frontend tests
  150. test_frontend_jsdom:
  151. COMPOSE_PROJECT_NAME=frontend_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_frontend_jsdom
  152. COMPOSE_PROJECT_NAME=frontend_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  153. test_frontend: test_frontend_jsdom test_frontend_ct
  154. #
  155. # Frontend component tests in Cypress
  156. #
  157. # Local development: use $ make test_frontend_ct
  158. #
  159. TEST_FRONTEND_CT_VARIANTS = \
  160. test_frontend_ct \
  161. test_frontend_ct_core_other \
  162. test_frontend_ct_core_features \
  163. test_frontend_ct_modules \
  164. test_frontend_ct_editor_other \
  165. test_frontend_ct_editor_visual \
  166. TEST_FRONTEND_CT_TARGETS = \
  167. $(TEST_FRONTEND_CT_VARIANTS) \
  168. build_test_frontend_ct \
  169. test_frontend_ct_ui \
  170. $(TEST_FRONTEND_CT_TARGETS): export USER_UID=$(shell id -u)
  171. $(TEST_FRONTEND_CT_TARGETS): export USER_GID=$(shell id -g)
  172. test_frontend_ct_ui:
  173. docker compose -f docker-compose.cypress.yml run $(DC_RUN_FLAGS) cypress run cypress:open-ct -- --browser chrome
  174. #
  175. # Writefull tests
  176. #
  177. test_writefull: export JUNIT_ROOT_SUITE_NAME = Writefull tests
  178. test_writefull:
  179. COMPOSE_PROJECT_NAME=writefull_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_writefull
  180. COMPOSE_PROJECT_NAME=writefull_test_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  181. # Note: The below cypress targets are for CI only
  182. build_test_frontend_ct:
  183. $(DOCKER_COMPOSE) build test_frontend_ct
  184. docker run --rm --volume /dev/shm:/dev/shm --user root $(IMAGE_CI) bash -ec 'for path in /overleaf/services/web/cypress/results /overleaf/services/web/cypress/support /overleaf/services/web/.cache; do mkdir -p $$path; chown -R node:node $$path; done && tar -cC / overleaf | tar -xC /dev/shm'
  185. test_frontend_ct_core_other: export CYPRESS_DOWNLOADS=./cypress/downloads/core
  186. test_frontend_ct_core_other: export CYPRESS_RESULTS=./cypress/results/core
  187. test_frontend_ct_core_other: export CYPRESS_SPEC_PATTERN=./test/frontend/**/*.spec.{js,jsx,ts,tsx}
  188. test_frontend_ct_core_other: export CYPRESS_EXCLUDE_SPEC_PATTERN=./test/frontend/features/**/*.spec.{js,jsx,ts,tsx}
  189. test_frontend_ct_core_features: export CYPRESS_DOWNLOADS=./cypress/downloads/core
  190. test_frontend_ct_core_features: export CYPRESS_RESULTS=./cypress/results/core
  191. test_frontend_ct_core_features: export CYPRESS_SPEC_PATTERN=./test/frontend/features/**/*.spec.{js,jsx,ts,tsx}
  192. test_frontend_ct_core_features: export CYPRESS_EXCLUDE_SPEC_PATTERN=./test/frontend/features/source-editor/**/*.spec.{js,jsx,ts,tsx}
  193. test_frontend_ct_modules: export CYPRESS_DOWNLOADS=./cypress/downloads/modules
  194. test_frontend_ct_modules: export CYPRESS_RESULTS=./cypress/results/modules
  195. test_frontend_ct_modules: export CYPRESS_SPEC_PATTERN=./modules/**/test/frontend/**/*.spec.{js,jsx,ts,tsx}
  196. test_frontend_ct_editor_other: export CYPRESS_DOWNLOADS=./cypress/downloads/editor_other
  197. test_frontend_ct_editor_other: export CYPRESS_RESULTS=./cypress/results/editor_other
  198. test_frontend_ct_editor_other: export CYPRESS_SPEC_PATTERN=./test/frontend/features/source-editor/**/*.spec.{js,jsx,ts,tsx}
  199. test_frontend_ct_editor_other: export CYPRESS_EXCLUDE_SPEC_PATTERN=./test/frontend/features/source-editor/components/codemirror-editor-visual*.spec.{js,jsx,ts,tsx}
  200. test_frontend_ct_editor_visual: export CYPRESS_DOWNLOADS=./cypress/downloads/editor_visual
  201. test_frontend_ct_editor_visual: export CYPRESS_RESULTS=./cypress/results/editor_visual
  202. test_frontend_ct_editor_visual: export CYPRESS_SPEC_PATTERN=./test/frontend/features/source-editor/components/codemirror-editor-visual*.spec.{js,jsx,ts,tsx}
  203. $(TEST_FRONTEND_CT_VARIANTS):
  204. COMPOSE_PROJECT_NAME=$@_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_frontend_ct
  205. COMPOSE_PROJECT_NAME=$@_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  206. #
  207. # Acceptance tests
  208. #
  209. test_acceptance: test_acceptance_app test_acceptance_modules
  210. test_acceptance_saas: test_acceptance_app_saas test_acceptance_modules_merged_saas
  211. test_acceptance_server_ce: test_acceptance_app_server_ce test_acceptance_modules_merged_server_ce
  212. test_acceptance_server_pro: test_acceptance_app_server_pro test_acceptance_modules_merged_server_pro
  213. TEST_ACCEPTANCE_APP := \
  214. test_acceptance_app_saas \
  215. test_acceptance_app_server_ce \
  216. test_acceptance_app_server_pro \
  217. ifeq ($(DEBUG),true)
  218. TEST_ACCEPTANCE_DEBUG_PORT := -p 127.0.0.1:19999:19999
  219. TEST_ACCEPTANCE_YARN_SCRIPT := yarn run --silent test:acceptance:app:debug
  220. else
  221. TEST_ACCEPTANCE_DEBUG_PORT :=
  222. TEST_ACCEPTANCE_YARN_SCRIPT := yarn run --silent test:acceptance:app
  223. endif
  224. test_acceptance_app: $(TEST_ACCEPTANCE_APP)
  225. test_acceptance_app_saas: export JUNIT_ROOT_SUITE_NAME = SaaS app acceptance tests
  226. test_acceptance_app_saas: export COMPOSE_PROJECT_NAME=acceptance_test_saas_$(BUILD_DIR_NAME)
  227. test_acceptance_app_saas: export OVERLEAF_CONFIG=$(CFG_SAAS)
  228. test_acceptance_app_server_ce: export JUNIT_ROOT_SUITE_NAME = Server CE app acceptance tests
  229. test_acceptance_app_server_ce: export COMPOSE_PROJECT_NAME=acceptance_test_server_ce_$(BUILD_DIR_NAME)
  230. test_acceptance_app_server_ce: export OVERLEAF_CONFIG=$(CFG_SERVER_CE)
  231. test_acceptance_app_server_pro: export JUNIT_ROOT_SUITE_NAME = Server Pro app acceptance tests
  232. test_acceptance_app_server_pro: export COMPOSE_PROJECT_NAME=acceptance_test_server_pro_$(BUILD_DIR_NAME)
  233. test_acceptance_app_server_pro: export OVERLEAF_CONFIG=$(CFG_SERVER_PRO)
  234. $(TEST_ACCEPTANCE_APP):
  235. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) $(TEST_ACCEPTANCE_DEBUG_PORT) test_acceptance $(TEST_ACCEPTANCE_YARN_SCRIPT)
  236. $(DOCKER_COMPOSE) down -v -t 0
  237. # We are using _make magic_ for turning these file-targets into calls to
  238. # sub-Makefiles in the individual modules.
  239. # These sub-Makefiles need to be kept in sync with the template, hence we
  240. # add a dependency on each modules Makefile and cross-link that to the
  241. # template at the very top of this file.
  242. # Example: `web$ make modules/server-ce-scripts/test_acceptance_server_ce`
  243. # Description: Run the acceptance tests of the server-ce-scripts module in an
  244. # Overleaf Community Edition Environment.
  245. # Break down:
  246. # Target: modules/server-ce-scripts/test_acceptance_server_ce
  247. # -> depends on modules/server-ce-scripts/Makefile
  248. # -> add environment variable BASE_CONFIG=$(CFG_SERVER_CE)
  249. # -> BASE_CONFIG=/overleaf/services/web/test/acceptance/config/settings.test.server-ce.js
  250. # -> automatic target: `make -C server-ce-scripts test_acceptance_server_ce`
  251. # -> automatic target: run `make test_acceptance_server_ce` in module
  252. # Target: modules/server-ce-scripts/Makefile
  253. # -> depends on Makefile.module
  254. # -> automatic target: copies the file when changed
  255. TEST_ACCEPTANCE_MODULES = $(MODULE_DIRS:=/test_acceptance)
  256. $(TEST_ACCEPTANCE_MODULES): %/test_acceptance: %/Makefile
  257. $(TEST_ACCEPTANCE_MODULES): modules/%/test_acceptance:
  258. $(MAKE) test_acceptance_module MODULE_NAME=$*
  259. TEST_ACCEPTANCE_MODULES_SAAS = $(MODULE_DIRS:=/test_acceptance_saas)
  260. $(TEST_ACCEPTANCE_MODULES_SAAS): %/test_acceptance_saas: %/Makefile
  261. $(TEST_ACCEPTANCE_MODULES_SAAS): export BASE_CONFIG = $(CFG_SAAS)
  262. # This line adds `/test_acceptance_saas` suffix to all items in $(MODULE_DIRS).
  263. TEST_ACCEPTANCE_MODULES_SERVER_CE = $(MODULE_DIRS:=/test_acceptance_server_ce)
  264. # This line adds a dependency on the modules Makefile.
  265. $(TEST_ACCEPTANCE_MODULES_SERVER_CE): %/test_acceptance_server_ce: %/Makefile
  266. # This line adds the environment variable BASE_CONFIG=$(CFG_SERVER_CE) to all
  267. # invocations of `web$ make modules/foo/test_acceptance_server_ce`.
  268. $(TEST_ACCEPTANCE_MODULES_SERVER_CE): export BASE_CONFIG = $(CFG_SERVER_CE)
  269. TEST_ACCEPTANCE_MODULES_SERVER_PRO = $(MODULE_DIRS:=/test_acceptance_server_pro)
  270. $(TEST_ACCEPTANCE_MODULES_SERVER_PRO): %/test_acceptance_server_pro: %/Makefile
  271. $(TEST_ACCEPTANCE_MODULES_SERVER_PRO): export BASE_CONFIG = $(CFG_SERVER_PRO)
  272. CLEAN_TEST_ACCEPTANCE_MODULES = $(MODULE_DIRS:=/clean_test_acceptance)
  273. $(CLEAN_TEST_ACCEPTANCE_MODULES): %/clean_test_acceptance: %/Makefile
  274. clean_test_acceptance_modules: $(CLEAN_TEST_ACCEPTANCE_MODULES)
  275. clean_ci: clean_test_acceptance_modules
  276. test_acceptance_module_noop:
  277. @echo
  278. @echo Module '$(MODULE_NAME)' does not run in ${LABEL}.
  279. @echo
  280. TEST_ACCEPTANCE_MODULE_MAYBE_IN := \
  281. test_acceptance_module_maybe_in_saas \
  282. test_acceptance_module_maybe_in_server_ce \
  283. test_acceptance_module_maybe_in_server_pro \
  284. test_acceptance_module: $(TEST_ACCEPTANCE_MODULE_MAYBE_IN)
  285. test_acceptance_module_maybe_in_saas: export BASE_CONFIG=$(CFG_SAAS)
  286. test_acceptance_module_maybe_in_server_ce: export BASE_CONFIG=$(CFG_SERVER_CE)
  287. test_acceptance_module_maybe_in_server_pro: export BASE_CONFIG=$(CFG_SERVER_PRO)
  288. # We need to figure out whether the module is loaded in a given environment.
  289. # This information is stored in the (base-)settings.
  290. # We get the full list of modules and check for a matching module entry.
  291. # Either the grep will find and emit the module, or exits with code 1, which
  292. # we handle with a fallback to a noop make target.
  293. # Run the node command in a docker compose container which provides the needed
  294. # yarn dependencies (from disk in dev-env or from the CI image in CI).
  295. # Pick the test_unit service which is very light-weight -- the test_acceptance
  296. # service would start mongo/redis.
  297. $(TEST_ACCEPTANCE_MODULE_MAYBE_IN): test_acceptance_module_maybe_in_%:
  298. $(MAKE) $(shell \
  299. OVERLEAF_CONFIG=$(BASE_CONFIG) \
  300. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_unit \
  301. yarn node test/acceptance/getModuleTargets test_acceptance_$* \
  302. | grep -e /$(MODULE_NAME)/ || echo test_acceptance_module_noop LABEL=$* \
  303. )
  304. # See docs for test_acceptance_server_ce how this works.
  305. test_acceptance_module_saas: export BASE_CONFIG = $(CFG_SAAS)
  306. test_acceptance_module_saas:
  307. $(MAKE) modules/$(MODULE_NAME)/test_acceptance_saas
  308. test_acceptance_module_server_ce: export BASE_CONFIG = $(CFG_SERVER_CE)
  309. test_acceptance_module_server_ce:
  310. $(MAKE) modules/$(MODULE_NAME)/test_acceptance_server_ce
  311. test_acceptance_module_server_pro: export BASE_CONFIG = $(CFG_SERVER_PRO)
  312. test_acceptance_module_server_pro:
  313. $(MAKE) modules/$(MODULE_NAME)/test_acceptance_server_pro
  314. # See docs for test_acceptance_server_ce how this works.
  315. TEST_ACCEPTANCE_MODULES_MERGED_INNER = $(MODULE_DIRS:=/test_acceptance_merged_inner)
  316. $(TEST_ACCEPTANCE_MODULES_MERGED_INNER): %/test_acceptance_merged_inner: %/Makefile
  317. test_acceptance_modules_merged_inner:
  318. $(MAKE) $(shell \
  319. OVERLEAF_CONFIG=$(BASE_CONFIG) \
  320. yarn node test/acceptance/getModuleTargets test_acceptance_merged_inner \
  321. )
  322. # inner loop for running saas tests in parallel
  323. no_more_targets:
  324. # If we ever have more than 50 modules, we need to add _6 targets to all the places and have it START at 51.
  325. test_acceptance_modules_merged_inner_1: export START=1
  326. test_acceptance_modules_merged_inner_2: export START=11
  327. test_acceptance_modules_merged_inner_3: export START=21
  328. test_acceptance_modules_merged_inner_4: export START=31
  329. test_acceptance_modules_merged_inner_5: export START=41
  330. TEST_ACCEPTANCE_MODULES_MERGED_INNER_SPLIT = \
  331. test_acceptance_modules_merged_inner_1 \
  332. test_acceptance_modules_merged_inner_2 \
  333. test_acceptance_modules_merged_inner_3 \
  334. test_acceptance_modules_merged_inner_4 \
  335. test_acceptance_modules_merged_inner_5 \
  336. # The node script prints one module per line.
  337. # Using tail and head we skip over the first n=START entries and print the last 10.
  338. # Finally we check with grep for any targets in a batch and print a fallback if none were found.
  339. $(TEST_ACCEPTANCE_MODULES_MERGED_INNER_SPLIT):
  340. $(MAKE) $(shell \
  341. OVERLEAF_CONFIG=$(BASE_CONFIG) \
  342. yarn node test/acceptance/getModuleTargets test_acceptance_merged_inner \
  343. | tail -n+$(START) | head -n 10 \
  344. | grep -e . || echo no_more_targets \
  345. )
  346. # See docs for test_acceptance_server_ce how this works.
  347. test_acceptance_modules_merged_saas: export JUNIT_ROOT_SUITE_NAME = SaaS modules acceptance tests
  348. test_acceptance_modules_merged_saas: export COMPOSE_PROJECT_NAME = \
  349. acceptance_test_modules_merged_saas_$(BUILD_DIR_NAME)
  350. test_acceptance_modules_merged_saas: export BASE_CONFIG = $(CFG_SAAS)
  351. test_acceptance_modules_merged_server_ce: export JUNIT_ROOT_SUITE_NAME = Server CE modules acceptance tests
  352. test_acceptance_modules_merged_server_ce: export COMPOSE_PROJECT_NAME = \
  353. acceptance_test_modules_merged_server_ce_$(BUILD_DIR_NAME)
  354. test_acceptance_modules_merged_server_ce: export BASE_CONFIG = $(CFG_SERVER_CE)
  355. test_acceptance_modules_merged_server_pro: export JUNIT_ROOT_SUITE_NAME = Server Pro modules acceptance tests
  356. test_acceptance_modules_merged_server_pro: export COMPOSE_PROJECT_NAME = \
  357. acceptance_test_modules_merged_server_pro_$(BUILD_DIR_NAME)
  358. test_acceptance_modules_merged_server_pro: export BASE_CONFIG = $(CFG_SERVER_PRO)
  359. # All these variants run the same command.
  360. # Each target has a different set of environment defined above.
  361. TEST_ACCEPTANCE_MODULES_MERGED_VARIANTS = \
  362. test_acceptance_modules_merged_saas \
  363. test_acceptance_modules_merged_server_ce \
  364. test_acceptance_modules_merged_server_pro \
  365. $(TEST_ACCEPTANCE_MODULES_MERGED_VARIANTS):
  366. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_acceptance yarn exec make test_acceptance_modules_merged_inner
  367. $(DOCKER_COMPOSE) down -v -t 0
  368. # outer loop for running saas tests in parallel
  369. TEST_ACCEPTANCE_MODULES_MERGED_SPLIT_SAAS = \
  370. test_acceptance_modules_merged_saas_1 \
  371. test_acceptance_modules_merged_saas_2 \
  372. test_acceptance_modules_merged_saas_3 \
  373. test_acceptance_modules_merged_saas_4 \
  374. test_acceptance_modules_merged_saas_5 \
  375. test_acceptance_modules_merged_saas_1: export COMPOSE_PROJECT_NAME = \
  376. acceptance_test_modules_merged_saas_1_$(BUILD_DIR_NAME)
  377. test_acceptance_modules_merged_saas_2: export COMPOSE_PROJECT_NAME = \
  378. acceptance_test_modules_merged_saas_2_$(BUILD_DIR_NAME)
  379. test_acceptance_modules_merged_saas_3: export COMPOSE_PROJECT_NAME = \
  380. acceptance_test_modules_merged_saas_3_$(BUILD_DIR_NAME)
  381. test_acceptance_modules_merged_saas_4: export COMPOSE_PROJECT_NAME = \
  382. acceptance_test_modules_merged_saas_4_$(BUILD_DIR_NAME)
  383. test_acceptance_modules_merged_saas_5: export COMPOSE_PROJECT_NAME = \
  384. acceptance_test_modules_merged_saas_5_$(BUILD_DIR_NAME)
  385. $(TEST_ACCEPTANCE_MODULES_MERGED_SPLIT_SAAS): export BASE_CONFIG = $(CFG_SAAS)
  386. $(TEST_ACCEPTANCE_MODULES_MERGED_SPLIT_SAAS): export JUNIT_ROOT_SUITE_NAME = SaaS modules acceptance tests
  387. $(TEST_ACCEPTANCE_MODULES_MERGED_SPLIT_SAAS): test_acceptance_modules_merged_saas_%:
  388. $(DOCKER_COMPOSE) run $(DC_RUN_FLAGS) test_acceptance yarn exec make test_acceptance_modules_merged_inner_$*
  389. $(DOCKER_COMPOSE) down -v -t 0
  390. test_acceptance_modules: $(TEST_ACCEPTANCE_MODULES_MERGED_VARIANTS)
  391. #
  392. # CI tests
  393. #
  394. rebase_coverage_reports:
  395. sed -i 's_path="/overleaf/_path="_' data/coverage/*/clover.xml
  396. # Create the bundle with full paths inside the monorepo to make it easier to organize them later.
  397. bundle_coverage_reports:
  398. cd ../../ && tar -czf services/web/data/coverage.tar.gz services/web/data/coverage/*/clover.xml
  399. ci:
  400. MOCHA_ARGS="--reporter tap" \
  401. $(MAKE) test
  402. #
  403. # Lint & format
  404. #
  405. # Run the linting commands in the scope of the monorepo.
  406. # Eslint and prettier (plus some configs) are on the root.
  407. RUN_LINTING_CI_MONOREPO = docker run --rm --volume $(MONOREPO)/.editorconfig:/overleaf/.editorconfig --volume $(MONOREPO)/eslint.config.mjs:/overleaf/eslint.config.mjs --volume $(MONOREPO)/.prettierignore:/overleaf/.prettierignore --volume $(MONOREPO)/.prettierrc.cjs:/overleaf/.prettierrc.cjs --volume $(MONOREPO)/tsconfig.backend.json:/overleaf/tsconfig.backend.json --volume $(MONOREPO)/services/web/data/reports:/overleaf/services/web/data/reports --volume $(MONOREPO)/.cache/:/overleaf/.cache/ -w /overleaf $(IMAGE_CI) yarn run --silent
  408. RUN_LINTING = ../../bin/run -w /overleaf/services/$(PROJECT_NAME) monorepo yarn run --silent
  409. RUN_LINTING_MONOREPO = ../../bin/run monorepo yarn run --silent
  410. ORG_PATH = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
  411. RUN_LINT_FORMAT ?= \
  412. docker run --rm --env BRANCH_NAME --env CI --env COMMIT_SHA --env MONOREPO --env PATH=/overleaf/node_modules/.bin:$(ORG_PATH) --volume $(MONOREPO)/.prettierignore:/overleaf/.prettierignore --volume $(MONOREPO)/.prettierrc.cjs:/overleaf/.prettierrc.cjs --volume ${PWD}/data/reports:/overleaf/services/web/data/reports --volume $(MONOREPO)/.cache/:/overleaf/.cache/ ${IMAGE_CI}
  413. NODE_MODULES_PATH := ${PATH}:${PWD}/node_modules/.bin:/overleaf/services/web/node_modules/.bin
  414. WITH_NODE_MODULES_PATH = \
  415. format_backend \
  416. format_frontend \
  417. format_misc \
  418. format_styles \
  419. format_test_app_unit \
  420. format_test_app_rest \
  421. format_test_modules \
  422. $(TEST_SUITES) \
  423. $(WITH_NODE_MODULES_PATH): export PATH=$(NODE_MODULES_PATH)
  424. WITH_CACHE_FOLDER = \
  425. lint_eslint \
  426. lint_stylelint \
  427. lint_in_docker \
  428. format_js \
  429. format_ci \
  430. format_fix \
  431. format_styles \
  432. format_styles_fix \
  433. format_pug \
  434. format_pug_fix \
  435. format_jenkins \
  436. format_jenkins_fix \
  437. format_in_docker \
  438. $(WITH_CACHE_FOLDER): ../../.cache/prettier/
  439. ../../.cache/prettier/:
  440. mkdir -p $@
  441. $(WITH_CACHE_FOLDER): ../../.cache/eslint/
  442. ../../.cache/eslint/:
  443. mkdir -p $@
  444. $(WITH_CACHE_FOLDER): ../../.cache/stylelint/
  445. ../../.cache/stylelint/:
  446. mkdir -p $@
  447. lint: lint_eslint
  448. lint_eslint:
  449. ifeq ($(CI),true)
  450. -yarn run lint --format json --output-file data/reports/eslint.json
  451. sed -i 's_"filePath":"/overleaf_"filePath":"$(MONOREPO)_g' data/reports/eslint.json
  452. else
  453. $(RUN_LINTING) lint
  454. endif
  455. lint_fix: lint_eslint_fix
  456. lint_eslint_fix:
  457. $(RUN_LINTING) lint:fix
  458. lint: lint_stylelint
  459. lint_stylelint:
  460. ifeq ($(CI),true)
  461. -yarn run lint:styles --formatter json --output-file data/reports/stylelint.json
  462. sed -i 's_"source":"/overleaf_"source":"$(MONOREPO)_g' data/reports/stylelint.json
  463. else
  464. $(RUN_LINTING) lint:styles
  465. endif
  466. lint_fix: lint_stylelint_fix
  467. lint_stylelint_fix:
  468. $(RUN_LINTING) lint:styles:fix
  469. lint: lint_pug
  470. lint_pug:
  471. bin/lint_pug_templates
  472. lint: lint_locales
  473. lint_locales:
  474. bin/lint_locales
  475. lint: check_extracted_translations
  476. check_extracted_translations:
  477. bin/check_extracted_translations
  478. cleanup_unused_locales:
  479. node scripts/translations/cleanupUnusedLocales.js
  480. lint: lint_flag_res_send_usage
  481. lint_flag_res_send_usage:
  482. bin/lint_flag_res_send_usage
  483. lint: lint_overleafModuleImports
  484. lint_overleafModuleImports:
  485. node scripts/check_overleafModuleImports.mjs
  486. lint: typecheck_frontend
  487. typecheck_frontend:
  488. ifeq ($(CI),true)
  489. yarn run --silent type-check
  490. else
  491. $(RUN_LINTING) type-check
  492. endif
  493. lint: typecheck_backend
  494. typecheck_backend:
  495. ifeq ($(CI),true)
  496. yarn run --silent type-check:backend
  497. else
  498. $(RUN_LINTING) type-check:backend
  499. endif
  500. lint_in_docker:
  501. $(RUN_LINT_FORMAT) make lint -j2 --output-sync
  502. format: format_js
  503. format_js:
  504. $(RUN_LINTING_MONOREPO) format services/web
  505. format_ci:
  506. $(RUN_LINTING_CI_MONOREPO) format services/web
  507. format_fix:
  508. $(RUN_LINTING_MONOREPO) format:fix services/web
  509. format_styles:
  510. $(RUN_LINTING_MONOREPO) format:styles
  511. format_styles_fix:
  512. $(RUN_LINTING_MONOREPO) format:styles:fix
  513. format_pug:
  514. $(RUN_LINTING_MONOREPO) format:pug
  515. format_pug_fix:
  516. $(RUN_LINTING_MONOREPO) format:pug:fix
  517. format_jenkins:
  518. $(RUN_LINTING_MONOREPO) format:jenkins
  519. format_jenkins_fix:
  520. $(RUN_LINTING_MONOREPO) format:jenkins:fix
  521. format_in_docker:
  522. $(RUN_LINT_FORMAT) bash -c 'cd /overleaf && yarn run format services/web'
  523. SHELLCHECK_OPTS = \
  524. --shell=bash \
  525. --external-sources
  526. SHELLCHECK_COLOR := $(if $(CI),--color=never,--color)
  527. SHELLCHECK_FILES := { git ls-files "*.sh" -z; git grep -Plz "\A\#\!.*bash"; } | sort -zu
  528. shellcheck:
  529. @$(SHELLCHECK_FILES) | xargs -0 -r docker run --rm -v $(PWD):/mnt -w /mnt \
  530. koalaman/shellcheck:stable $(SHELLCHECK_OPTS) $(SHELLCHECK_COLOR)
  531. shellcheck_fix:
  532. @$(SHELLCHECK_FILES) | while IFS= read -r -d '' file; do \
  533. diff=$$(docker run --rm -v $(PWD):/mnt -w /mnt koalaman/shellcheck:stable $(SHELLCHECK_OPTS) --format=diff "$$file" 2>/dev/null); \
  534. if [ -n "$$diff" ] && ! echo "$$diff" | patch -p1 >/dev/null 2>&1; then echo "\033[31m$$file\033[0m"; \
  535. elif [ -n "$$diff" ]; then echo "$$file"; \
  536. else echo "\033[2m$$file\033[0m"; fi \
  537. done
  538. #
  539. # Build & publish
  540. #
  541. IMAGE_CI ?= ci/$(PROJECT_NAME):$(BRANCH_NAME_TAG_SAFE)-$(BUILD_NUMBER)
  542. IMAGE_REPO ?= us-east1-docker.pkg.dev/overleaf-ops/ol-docker/$(PROJECT_NAME)
  543. IMAGE_REPO_FINAL ?= $(IMAGE_REPO):$(BRANCH_NAME_TAG_SAFE)-$(BUILD_NUMBER)
  544. # A shared scratch tag that is used by all the pipelines for pushing the layers of the production image ASAP from a parallel step.
  545. # We do not want to make the image available before all the tests have passed.
  546. # Using a single tag avoids generating cruft in our docker repository / AR.
  547. IMAGE_SCRATCH ?= $(IMAGE_REPO):do-not-use-this-tag-for-deploys--it-is-used-for-early-pushes-in-ci
  548. IMAGE_CACHE ?= $(IMAGE_REPO):cache-$(shell cat \
  549. $(MONOREPO)/package.json \
  550. $(MONOREPO)/yarn.lock \
  551. $(MONOREPO)/libraries/access-token-encryptor/package.json \
  552. $(MONOREPO)/libraries/eslint-plugin/package.json \
  553. $(MONOREPO)/libraries/fetch-utils/package.json \
  554. $(MONOREPO)/libraries/logger/package.json \
  555. $(MONOREPO)/libraries/metrics/package.json \
  556. $(MONOREPO)/libraries/mongo-utils/package.json \
  557. $(MONOREPO)/libraries/o-error/package.json \
  558. $(MONOREPO)/libraries/object-persistor/package.json \
  559. $(MONOREPO)/libraries/overleaf-editor-core/package.json \
  560. $(MONOREPO)/libraries/promise-utils/package.json \
  561. $(MONOREPO)/libraries/ranges-tracker/package.json \
  562. $(MONOREPO)/libraries/redis-wrapper/package.json \
  563. $(MONOREPO)/libraries/settings/package.json \
  564. $(MONOREPO)/libraries/stream-utils/package.json \
  565. $(MONOREPO)/libraries/validation-tools/package.json \
  566. $(MONOREPO)/services/web/package.json \
  567. $(MONOREPO)/services/web/scripts/fetch-pyodide-packages.mjs \
  568. $(MONOREPO)/patches/* \
  569. | sha256sum | cut -d '-' -f1)
  570. export SENTRY_RELEASE ?= ${COMMIT_SHA}
  571. build_deps:
  572. docker build --pull \
  573. --build-arg BUILDKIT_INLINE_CACHE=1 \
  574. --cache-from $(IMAGE_CACHE) \
  575. --tag $(IMAGE_CACHE) \
  576. --target deps \
  577. --file Dockerfile \
  578. ../..
  579. build_dev:
  580. docker build \
  581. --build-arg SENTRY_RELEASE \
  582. --tag $(IMAGE_CI) \
  583. --tag $(IMAGE_CI)-dev \
  584. --target dev \
  585. --file Dockerfile \
  586. ../..
  587. build_webpack:
  588. $(MAKE) build_webpack_once \
  589. || $(MAKE) build_webpack_once
  590. build_webpack_once:
  591. docker build \
  592. --build-arg SENTRY_RELEASE \
  593. --tag $(IMAGE_CI)-webpack \
  594. --target webpack \
  595. --file Dockerfile \
  596. ../..
  597. build_pug:
  598. docker build \
  599. --build-arg SENTRY_RELEASE \
  600. --tag $(IMAGE_CI)-pug \
  601. --target pug \
  602. --file Dockerfile \
  603. ../..
  604. build:
  605. docker build \
  606. --build-arg SENTRY_RELEASE \
  607. --tag $(IMAGE_REPO_FINAL) \
  608. --tag $(IMAGE_SCRATCH) \
  609. --target app \
  610. --file Dockerfile \
  611. ../..
  612. publish:
  613. docker push $(IMAGE_REPO_FINAL)
  614. push_branch:
  615. docker push $(IMAGE_CACHE)
  616. push_scratch:
  617. docker push $(IMAGE_SCRATCH)
  618. SENTRY_IMAGE=getsentry/sentry-cli:2.16.1
  619. sentry_prefetch:
  620. docker pull $(SENTRY_IMAGE)
  621. sentry_upload:
  622. docker run --rm \
  623. --volume $(MONOREPO):$(MONOREPO) --workdir $(PWD) \
  624. --user "$(shell id -u):$(shell id -g)" \
  625. --env BRANCH_NAME --env SENTRY_RELEASE \
  626. --entrypoint bin/sentry_upload \
  627. $(SENTRY_IMAGE)
  628. tar:
  629. COMPOSE_PROJECT_NAME=tar_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) run --rm tar
  630. COMPOSE_PROJECT_NAME=tar_$(BUILD_DIR_NAME) $(DOCKER_COMPOSE) down -v -t 0
  631. build_storybook:
  632. yarn run lezer-latex:generate
  633. mkdir -p data/storybook/
  634. rm -rf data/storybook/$(BRANCH_NAME)
  635. yarn run build-storybook --output-dir data/storybook/$(BRANCH_NAME) --loglevel warn --quiet
  636. storybook_index:
  637. mkdir -p data/storybook/
  638. m4 \
  639. -DLIST="$(shell gsutil ls "${BUCKET}/" \
  640. | sed -E "s@^${BUCKET}/([^/]+)/@\1@" \
  641. | grep -v -e index.html \
  642. | sort \
  643. | tr '\n' ,)" \
  644. -DBRANCH_NAME="${BRANCH_NAME}" .storybook/index.html.m4 \
  645. > data/storybook/index.html
  646. MODULE_TARGETS = \
  647. $(TEST_ACCEPTANCE_MODULES_SAAS) \
  648. $(TEST_ACCEPTANCE_MODULES_SERVER_CE) \
  649. $(TEST_ACCEPTANCE_MODULES_SERVER_PRO) \
  650. $(TEST_ACCEPTANCE_MODULES_MERGED_INNER) \
  651. $(CLEAN_TEST_ACCEPTANCE_MODULES) \
  652. $(TEST_UNIT_MODULES) \
  653. $(MODULE_TARGETS):
  654. $(MAKE) -C $(dir $@) $(notdir $@) BUILD_DIR_NAME=$(BUILD_DIR_NAME)
  655. .PHONY:
  656. $(MODULE_TARGETS) \
  657. compile_modules compile_modules_full clean_ci \
  658. test test_module mongo_migrations_for_tests test_unit test_unit_app \
  659. test_unit_modules test_unit_module test_frontend_jsdom \
  660. test_acceptance test_acceptance_app test_acceptance_modules \
  661. test_acceptance_module ci format format_fix lint \
  662. shellcheck shellcheck_fix \
  663. build publish tar