forked from yuriabrito/Will.IAM
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathMakefile
172 lines (140 loc) · 5.1 KB
/
Makefile
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
testable_packages=$(shell go list ./... | egrep -v 'constants|mocks|testing')
project=$(shell basename $(PWD))
test_db_name=${project}-test
pg_docker_image=$(project)_postgres_1
db_url=postgres://postgres:$(project)@localhost:8432/$(project)?sslmode=disable
db_test_url=postgres://postgres:$(project)@localhost:8432/$(test_db_name)?sslmode=disable
uname_S=$(shell uname -s)
# TravisCI runs in Linux instances, while developers run in MacOS machines
ifeq ($(uname_S), Darwin)
platform := darwin
else
platform := linux
endif
# TODO(gerson-scanapieco): Pass arguments to make targets to reduce duplication, e.g. db/create and db/create-test
# becoming make db/create DB_URL=...
.PHONY: all
all: setup/migrate download-mod db/setup
.PHONY: ci/install
ci/install: setup/migrate download-mod db/setup-test
.PHONY: build
build:
@mkdir -p bin && go build -o ./bin/$(project) .
.PHONY: docker/build
docker/build:
@docker build -t $(project) .
.PHONY: run
run: build
./bin/Will.IAM start-api --host=localhost -v3
.PHONY: dev-run
dev-run:
@reflex -c reflex.conf -- sh -c make run
.PHONY: test
test: db/setup-test test/unit test/integration db/stop-test
# Installs the golang-migrate dependency if its not already installed.
.PHONY: setup/migrate
setup/migrate:
ifeq ($(shell command -v migrate),)
@echo "Installing migrate..."
@curl -L https://github.com/golang-migrate/migrate/releases/download/v4.4.0/migrate.$(platform)-amd64.tar.gz | tar xvz
@mv migrate.$(platform)-amd64 $(GOPATH)/bin/migrate
@echo "Done"
else
@echo "migrate is already installed. Skipping..."
endif
.PHONY: download-mod
download-mod:
@go mod download
# stop all containers, removing container data
.PHONY: compose-down
compose-down:
@docker-compose down
# stop all containers, preserving container data
.PHONY: compose-stop
compose-stop:
@docker-compose stop
# start all containers
.PHONY: compose-up
compose-up:
@docker-compose up william
# start only the dependency containers
.PHONY: dependencies/up
dependencies/up:
@mkdir -p docker_data && docker-compose up -d postgres oauth2-server
@until docker inspect --format "{{json .State.Health.Status }}" Will.IAM_postgres_1 | grep -q "healthy"; do echo 'Waiting Postgres...' && sleep 1; done
@until docker inspect --format "{{json .State.Health.Status }}" Will.IAM_oauth2_server_1 | grep -q "healthy"; do echo 'Waiting OAuth2 server...' && sleep 1; done
@sleep 2
.PHONY: db/setup
db/setup: db/up db/create-user db/create db/migrate
.PHONY: db/setup-test
db/setup-test: db/up db/create-user db/create-test db/migrate-test
# start only the database container
.PHONY: db/up
db/up:
@mkdir -p docker_data && docker-compose up -d postgres
@until docker inspect --format "{{json .State.Health.Status }}" Will.IAM_postgres_1 | grep -q "healthy"; do echo 'Waiting Postgres...' && sleep 1; done
@sleep 2
.PHONY: db/create-user
db/create-user:
@docker exec $(pg_docker_image) createuser -s -U postgres $(project) 2>/dev/null || true
.PHONY: db/create
db/create:
@docker exec $(pg_docker_image) createdb -U $(project) $(project) 2>/dev/null || true
.PHONY: db/create-test
db/create-test:
@docker exec $(pg_docker_image) createdb -U $(project) $(test_db_name) 2>/dev/null || true
@sleep 2
.PHONY: db/stop-test
db/stop-test: db/drop-test compose-down
.PHONY: db/drop-test
db/drop-test:
@migrate -path migrations -database ${db_test_url} drop
.PHONY: db/migrate
db/migrate:
@migrate -path migrations -database ${db_url} up
.PHONY: db/migrate-test
db/migrate-test:
@migrate -path migrations -database ${db_test_url} up
.PHONY: db-drop
db-drop:
@migrate -path migrations -database ${db_url} drop
.PHONY: test/unit
test/unit:
@echo "Unit Tests"
@go test ${testable_packages} -tags=unit -coverprofile unit.coverprofile -v
@make gather-unit-profiles
.PHONY: test/integration
test/integration:
@echo "Integration Tests"
@ret=0 && for pkg in ${testable_packages}; do \
echo $$pkg; \
go test $$pkg -tags=integration -coverprofile integration.coverprofile -v; \
test $$? -eq 0 || ret=1; \
done; exit $$ret
@make gather-integration-profiles
.PHONY: ci/test
ci/test:
@echo "Unit Tests - START"
@go test ${testable_packages} -tags=unit -covermode=count -coverprofile=coverage.out -v -p 1
@echo "Unit Tests - DONE"
@echo "Integration Tests - START"
@go test ${testable_packages} -tags=integration -covermode=count -coverprofile=coverage.out -v -p 1
@echo "Integration Tests - DONE"
.PHONY: gather-unit-profiles
gather-unit-profiles:
@mkdir -p _build
@echo "mode: count" > _build/coverage-unit.out
@bash -c 'for f in $$(find . -type d -name "docker_data" -prune -o -type f \
-name "*.coverprofile" -print); \
do tail -n +2 $$f >> _build/coverage-unit.out; done'
@find . -type d -name "docker_data" -prune -o \
-name "*.coverprofile" -exec rm {} +
.PHONY: gather-integration-profiles
gather-integration-profiles:
@mkdir -p _build
@echo "mode: count" > _build/coverage-integration.out
@bash -c 'for f in $$(find . -type d -name "docker_data" -prune -o -type f \
-name "*.coverprofile" -print); \
do tail -n +2 $$f >> _build/coverage-integration.out; done'
@find . -type d -name "docker_data" -prune -o \
-name "*.coverprofile" -exec rm {} +