You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

289 lines
8.5 KiB

9 years ago
9 years ago
Add basic integration test infrastructure (and new endpoint `/api/v1/version` for testing it) (#741) * Implement '/api/v1/version' * Cleanup and various fixes * Enhance run.sh * Add install_test.go * Add parameter utils.Config for testing handlers * Re-organize TestVersion.go * Rename functions * handling process cleanup properly * Fix missing function renaming * Cleanup the 'retry' logic * Cleanup * Remove unneeded logging code * Logging messages tweaking * Logging message tweaking * Fix logging messages * Use 'const' instead of hardwired numbers * We don't really need retries anymore * Move constant ServerHttpPort to install_test.go * Restore mistakenly removed constant * Add required comments to make the linter happy. * Fix comments and naming to address linter's complaints * Detect Gitea executale version automatically * Remove tests/run.sh, `go test` suffices. * Make `make build` a prerequisite of `make test` * Do not sleep before trying * Speedup the server pinging loop * Use defined const instead of hardwired numbers * Remove redundant error handling * Use a dedicated target for running code.gitea.io/tests * Do not make 'test' depend on 'build' target * Rectify the excluded package list * Remove redundant 'exit 1' * Change the API to allow passing test.T to test handlers * Make testing.T an embedded field * Use assert.Equal to comparing results * Add copyright info * Parametrized logging output * Use tmpdir instead * Eliminate redundant casting * Remove unneeded variable * Fix last commit * Add missing copyright info * Replace fmt.Fprintf with fmt.Fprint * rename the xtest to integration-test * Use Symlink instead of hard-link for cross-device linking * Turn debugging logs on * Follow the existing framework for APIs * Output logs only if test.v is true * Re-order import statements * Enhance the error message * Fix comment which breaks the linter's rule * Rename 'integration-test' to 'e2e-test' for saving keystrokes * Add comment to avoid possible confusion * Rename tests -> integration-tests Also change back the Makefile to use `make integration-test`. * Use tests/integration for now * tests/integration -> integrations Slightly flattened directory hierarchy is better. * Update Makefile accordingly * Fix a missing change in Makefile * govendor update code.gitea.io/sdk/gitea * Fix comment of struct fields * Fix conditional nonsense * Fix missing updates regarding version string changes * Make variable naming more consistent * Check http status code * Rectify error messages
7 years ago
9 years ago
9 years ago
  1. DIST := dist
  2. IMPORT := code.gitea.io/gitea
  3. SED_INPLACE := sed -i
  4. ifeq ($(OS), Windows_NT)
  5. EXECUTABLE := gitea.exe
  6. else
  7. EXECUTABLE := gitea
  8. UNAME_S := $(shell uname -s)
  9. ifeq ($(UNAME_S),Darwin)
  10. SED_INPLACE := sed -i ''
  11. endif
  12. endif
  13. BINDATA := modules/{options,public,templates}/bindata.go
  14. STYLESHEETS := $(wildcard public/less/index.less public/less/_*.less)
  15. JAVASCRIPTS :=
  16. DOCKER_TAG := gitea/gitea:latest
  17. GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*" -not -path "*/bindata.go")
  18. GOFMT ?= gofmt -s
  19. GOFLAGS := -i -v
  20. EXTRA_GOFLAGS ?=
  21. LDFLAGS := -X "main.Version=$(shell git describe --tags --always | sed 's/-/+/' | sed 's/^v//')" -X "main.Tags=$(TAGS)"
  22. PACKAGES ?= $(filter-out code.gitea.io/gitea/integrations,$(shell go list ./... | grep -v /vendor/))
  23. SOURCES ?= $(shell find . -name "*.go" -type f)
  24. TAGS ?=
  25. TMPDIR := $(shell mktemp -d 2>/dev/null || mktemp -d -t 'gitea-temp')
  26. ifeq ($(OS), Windows_NT)
  27. EXECUTABLE := gitea.exe
  28. else
  29. EXECUTABLE := gitea
  30. endif
  31. ifneq ($(DRONE_TAG),)
  32. VERSION ?= $(subst v,,$(DRONE_TAG))
  33. else
  34. ifneq ($(DRONE_BRANCH),)
  35. VERSION ?= $(subst release/v,,$(DRONE_BRANCH))
  36. else
  37. VERSION ?= master
  38. endif
  39. endif
  40. .PHONY: all
  41. all: build
  42. .PHONY: clean
  43. clean:
  44. go clean -i ./...
  45. rm -rf $(EXECUTABLE) $(DIST) $(BINDATA)
  46. required-gofmt-version:
  47. @go version | grep -q '\(1.7\|1.8\)' || { echo "We require go version 1.7 or 1.8 to format code" >&2 && exit 1; }
  48. .PHONY: fmt
  49. fmt: required-gofmt-version
  50. $(GOFMT) -w $(GOFILES)
  51. .PHONY: vet
  52. vet:
  53. go vet $(PACKAGES)
  54. .PHONY: generate
  55. generate:
  56. @hash go-bindata > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  57. go get -u github.com/jteeuwen/go-bindata/...; \
  58. fi
  59. go generate $(PACKAGES)
  60. .PHONY: generate-swagger
  61. generate-swagger:
  62. @hash swagger > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  63. go get -u github.com/go-swagger/go-swagger/cmd/swagger; \
  64. fi
  65. swagger generate spec -o ./public/swagger.v1.json
  66. $(SED_INPLACE) "s;\".ref\": \"#/definitions/GPGKey\";\"type\": \"object\";g" ./public/swagger.v1.json
  67. $(SED_INPLACE) "s;^ \".ref\": \"#/definitions/Repository\"; \"type\": \"object\";g" ./public/swagger.v1.json
  68. .PHONY: errcheck
  69. errcheck:
  70. @hash errcheck > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  71. go get -u github.com/kisielk/errcheck; \
  72. fi
  73. errcheck $(PACKAGES)
  74. .PHONY: lint
  75. lint:
  76. @hash golint > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  77. go get -u github.com/golang/lint/golint; \
  78. fi
  79. for PKG in $(PACKAGES); do golint -set_exit_status $$PKG || exit 1; done;
  80. .PHONY: misspell-check
  81. misspell-check:
  82. @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  83. go get -u github.com/client9/misspell/cmd/misspell; \
  84. fi
  85. misspell -error -i unknwon $(GOFILES)
  86. .PHONY: misspell
  87. misspell:
  88. @hash misspell > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  89. go get -u github.com/client9/misspell/cmd/misspell; \
  90. fi
  91. misspell -w -i unknwon $(GOFILES)
  92. .PHONY: fmt-check
  93. fmt-check: required-gofmt-version
  94. # get all go files and run go fmt on them
  95. @diff=$$($(GOFMT) -d $(GOFILES)); \
  96. if [ -n "$$diff" ]; then \
  97. echo "Please run 'make fmt' and commit the result:"; \
  98. echo "$${diff}"; \
  99. exit 1; \
  100. fi;
  101. .PHONY: test
  102. test: fmt-check
  103. go test $(PACKAGES)
  104. .PHONY: test-coverage
  105. test-coverage: unit-test-coverage integration-test-coverage
  106. for PKG in $(PACKAGES); do\
  107. touch $$GOPATH/src/$$PKG/coverage.out;\
  108. egrep "$$PKG[^/]*\.go" integration.coverage.out > int.coverage.out;\
  109. gocovmerge $$GOPATH/src/$$PKG/coverage.out int.coverage.out > pkg.coverage.out;\
  110. mv pkg.coverage.out $$GOPATH/src/$$PKG/coverage.out;\
  111. rm int.coverage.out;\
  112. done;
  113. .PHONY: unit-test-coverage
  114. unit-test-coverage:
  115. for PKG in $(PACKAGES); do go test -cover -coverprofile $$GOPATH/src/$$PKG/coverage.out $$PKG || exit 1; done;
  116. .PHONY: test-vendor
  117. test-vendor:
  118. @hash govendor > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  119. go get -u github.com/kardianos/govendor; \
  120. fi
  121. govendor list +unused | tee "$(TMPDIR)/wc-gitea-unused"
  122. [ $$(cat "$(TMPDIR)/wc-gitea-unused" | wc -l) -eq 0 ] || echo "Warning: /!\\ Some vendor are not used /!\\"
  123. govendor list +outside | tee "$(TMPDIR)/wc-gitea-outside"
  124. [ $$(cat "$(TMPDIR)/wc-gitea-outside" | wc -l) -eq 0 ] || exit 1
  125. govendor status || exit 1
  126. .PHONY: test-sqlite
  127. test-sqlite: integrations.sqlite.test
  128. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test
  129. .PHONY: test-mysql
  130. test-mysql: integrations.test
  131. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test
  132. .PHONY: test-pgsql
  133. test-pgsql: integrations.test
  134. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test
  135. .PHONY: bench-sqlite
  136. bench-sqlite: integrations.sqlite.test
  137. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/sqlite.ini ./integrations.sqlite.test -test.bench .
  138. .PHONY: bench-mysql
  139. bench-mysql: integrations.test
  140. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.test -test.bench .
  141. .PHONY: bench-pgsql
  142. bench-pgsql: integrations.test
  143. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/pgsql.ini ./integrations.test -test.bench .
  144. .PHONY: integration-test-coverage
  145. integration-test-coverage: integrations.cover.test
  146. GITEA_ROOT=${CURDIR} GITEA_CONF=integrations/mysql.ini ./integrations.cover.test -test.coverprofile=integration.coverage.out
  147. integrations.test: $(SOURCES)
  148. go test -c code.gitea.io/gitea/integrations
  149. integrations.sqlite.test: $(SOURCES)
  150. go test -c code.gitea.io/gitea/integrations -o integrations.sqlite.test -tags 'sqlite'
  151. integrations.cover.test: $(SOURCES)
  152. go test -c code.gitea.io/gitea/integrations -coverpkg $(shell echo $(PACKAGES) | tr ' ' ',') -o integrations.cover.test
  153. .PHONY: check
  154. check: test
  155. .PHONY: install
  156. install: $(wildcard *.go)
  157. go install -v -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)'
  158. .PHONY: build
  159. build: $(EXECUTABLE)
  160. $(EXECUTABLE): $(SOURCES)
  161. go build $(GOFLAGS) $(EXTRA_GOFLAGS) -tags '$(TAGS)' -ldflags '-s -w $(LDFLAGS)' -o $@
  162. .PHONY: docker
  163. docker:
  164. docker run -ti --rm -v $(CURDIR):/srv/app/src/code.gitea.io/gitea -w /srv/app/src/code.gitea.io/gitea -e TAGS="bindata $(TAGS)" webhippie/golang:edge make clean generate build
  165. docker build -t $(DOCKER_TAG) .
  166. .PHONY: release
  167. release: release-dirs release-windows release-linux release-darwin release-copy release-check
  168. .PHONY: release-dirs
  169. release-dirs:
  170. mkdir -p $(DIST)/binaries $(DIST)/release
  171. .PHONY: release-windows
  172. release-windows:
  173. @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  174. go get -u github.com/karalabe/xgo; \
  175. fi
  176. xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'windows/*' -out gitea-$(VERSION) .
  177. ifeq ($(CI),drone)
  178. mv /build/* $(DIST)/binaries
  179. endif
  180. .PHONY: release-linux
  181. release-linux:
  182. @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  183. go get -u github.com/karalabe/xgo; \
  184. fi
  185. xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '-linkmode external -extldflags "-static" $(LDFLAGS)' -targets 'linux/*' -out gitea-$(VERSION) .
  186. ifeq ($(CI),drone)
  187. mv /build/* $(DIST)/binaries
  188. endif
  189. .PHONY: release-darwin
  190. release-darwin:
  191. @hash xgo > /dev/null 2>&1; if [ $$? -ne 0 ]; then \
  192. go get -u github.com/karalabe/xgo; \
  193. fi
  194. xgo -dest $(DIST)/binaries -tags 'netgo $(TAGS)' -ldflags '$(LDFLAGS)' -targets 'darwin/*' -out gitea-$(VERSION) .
  195. ifeq ($(CI),drone)
  196. mv /build/* $(DIST)/binaries
  197. endif
  198. .PHONY: release-copy
  199. release-copy:
  200. $(foreach file,$(wildcard $(DIST)/binaries/$(EXECUTABLE)-*),cp $(file) $(DIST)/release/$(notdir $(file));)
  201. .PHONY: release-check
  202. release-check:
  203. cd $(DIST)/release; $(foreach file,$(wildcard $(DIST)/release/$(EXECUTABLE)-*),sha256sum $(notdir $(file)) > $(notdir $(file)).sha256;)
  204. .PHONY: javascripts
  205. javascripts: public/js/index.js
  206. .IGNORE: public/js/index.js
  207. public/js/index.js: $(JAVASCRIPTS)
  208. cat $< >| $@
  209. .PHONY: stylesheets
  210. stylesheets: public/css/index.css
  211. .IGNORE: public/css/index.css
  212. public/css/index.css: $(STYLESHEETS)
  213. lessc $< $@
  214. .PHONY: swagger-ui
  215. swagger-ui:
  216. rm -Rf public/assets/swagger-ui
  217. git clone --depth=10 -b v3.0.7 --single-branch https://github.com/swagger-api/swagger-ui.git /tmp/swagger-ui
  218. mv /tmp/swagger-ui/dist public/assets/swagger-ui
  219. rm -Rf /tmp/swagger-ui
  220. $(SED_INPLACE) "s;http://petstore.swagger.io/v2/swagger.json;../../swagger.v1.json;g" public/assets/swagger-ui/index.html
  221. .PHONY: assets
  222. assets: javascripts stylesheets
  223. .PHONY: update-translations
  224. update-translations:
  225. mkdir -p ./translations
  226. cd ./translations && curl -L https://crowdin.com/download/project/gitea.zip > gitea.zip && unzip gitea.zip
  227. rm ./translations/gitea.zip
  228. $(SED_INPLACE) -e 's/="/=/g' -e 's/"$$//g' ./translations/*.ini
  229. $(SED_INPLACE) -e 's/\\"/"/g' ./translations/*.ini
  230. mv ./translations/*.ini ./options/locale/
  231. rmdir ./translations