# Copyright CloudQuery Authors
# This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
# If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.

# Unit tests
.PHONY: run-test
run-test:
	FIRESTORE_EMULATOR_HOST=localhost:8080 go test -v -race -timeout 3m ./...

.PHONY: test
test: setup-firestore run-test teardown-firestore

.PHONY: lint
lint:
	golangci-lint run --config ../../.golangci.yml

.PHONY: gen-docs
gen-docs:
	echo "skipping docs generation for firestore source plugin"

.PHONY: gen-licenses
gen-licenses:
ifndef CI
	go install github.com/google/go-licenses@v1.6.0
	go run github.com/cloudquery/licenser@v0.2.0 report .
endif

.PHONY: gen-spec-schema
gen-spec-schema:
	go run client/spec/gen/main.go

# All gen targets
.PHONY: gen
gen: gen-docs gen-spec-schema gen-licenses

.PHONY: setup-firestore
setup-firestore:
	docker compose up --wait

.PHONY: teardown-firestore
teardown-firestore:
	docker compose down --volumes --remove-orphans

.PHONY: gen-coverage-report
gen-coverage-report:
	FIRESTORE_EMULATOR_HOST=localhost:8080 go test -timeout 3m -coverprofile=coverage.out.tmp ./...
	cat coverage.out.tmp | grep -vE "MockGen|codegen|mocks" > coverage.out
	rm coverage.out.tmp
	echo "| File | Function | Coverage |" > coverage.md
	echo "| --- | --- | --- |" >> coverage.md
	go tool cover -func=coverage.out | tail -n +2 | while read line; do \
		file=$$(echo $$line | awk '{print $$1}'); \
		func=$$(echo $$line | awk '{print $$2}'); \
		cov=$$(echo $$line | awk '{print $$3}'); \
		printf "| %s | %s | %s |\\n" "$$file" "$$func" "$$cov" >> coverage.md; \
	done
	rm coverage.out

.PHONY: coverage
coverage: setup-firestore gen-coverage-report teardown-firestore
