Skip to content

Commit 0e87943

Browse files
authored
chore: Add build.sh common build script. (#1321)
Adds the a standardized developer commands script "build.sh" that will exist in this and all other Cloud SQL Connector projects. GoogleCloudPlatform/cloud-sql-jdbc-socket-factory#2122
1 parent 29f8da1 commit 0e87943

File tree

10 files changed

+188
-9
lines changed

10 files changed

+188
-9
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ dist/
88
.idea
99
.coverage
1010
sponge_log.xml
11+
.envrc
12+
*.iml

build.sh

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2025 Google LLC.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http=//www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Set SCRIPT_DIR to the current directory of this file.
18+
SCRIPT_DIR=$(cd -P "$(dirname "$0")" >/dev/null 2>&1 && pwd)
19+
SCRIPT_FILE="${SCRIPT_DIR}/$(basename "$0")"
20+
21+
##
22+
## Local Development
23+
##
24+
## These functions should be used to run the local development process
25+
##
26+
27+
if [[ ! -d venv ]] ; then
28+
echo "./venv not found. Setting up venv"
29+
python3 -m venv "$PWD/venv"
30+
fi
31+
source "$PWD/venv/bin/activate"
32+
33+
if which pip3 ; then
34+
PIP_CMD=pip3
35+
elif which pip ; then
36+
PIP_CMD=pip
37+
else
38+
echo "pip not found. Please add pip to your path."
39+
exit 1
40+
fi
41+
if ! which nox ; then
42+
$PIP_CMD install nox
43+
fi
44+
45+
46+
47+
## clean - Cleans the build output
48+
function clean() {
49+
if [[ -d '.tools' ]] ; then
50+
rm -rf .tools
51+
fi
52+
}
53+
54+
## build - Builds the project without running tests.
55+
function build() {
56+
nox --sessions build
57+
}
58+
59+
## test - Runs local unit tests.
60+
function test() {
61+
nox --sessions unit --python=3.13
62+
}
63+
64+
## e2e - Runs end-to-end integration tests.
65+
function e2e() {
66+
if [[ ! -f .envrc ]] ; then
67+
write_e2e_env .envrc
68+
fi
69+
source .envrc
70+
nox --sessions system --python=3.13
71+
}
72+
73+
## fix - Fixes code format.
74+
function fix() {
75+
nox --sessions format
76+
}
77+
78+
## lint - runs the linters
79+
function lint() {
80+
# Check the commit includes a go.mod that is fully
81+
# up to date.
82+
nox --sessions lint
83+
}
84+
85+
## deps - updates project dependencies to latest
86+
function deps() {
87+
echo "Todo: deps"
88+
exit 1
89+
}
90+
91+
# write_e2e_env - Loads secrets from the gcloud project and writes
92+
# them to target/e2e.env to run e2e tests.
93+
function write_e2e_env(){
94+
# All secrets used by the e2e tests in the form <env_name>=<secret_name>
95+
secret_vars=(
96+
MYSQL_CONNECTION_NAME=MYSQL_CONNECTION_NAME
97+
MYSQL_USER=MYSQL_USER
98+
MYSQL_USER_IAM=MYSQL_USER_IAM_GO
99+
MYSQL_PASS=MYSQL_PASS
100+
MYSQL_DB=MYSQL_DB
101+
MYSQL_MCP_CONNECTION_NAME=MYSQL_MCP_CONNECTION_NAME
102+
MYSQL_MCP_PASS=MYSQL_MCP_PASS
103+
POSTGRES_CONNECTION_NAME=POSTGRES_CONNECTION_NAME
104+
POSTGRES_USER=POSTGRES_USER
105+
POSTGRES_USER_IAM=POSTGRES_USER_IAM_GO
106+
POSTGRES_PASS=POSTGRES_PASS
107+
POSTGRES_DB=POSTGRES_DB
108+
POSTGRES_CAS_CONNECTION_NAME=POSTGRES_CAS_CONNECTION_NAME
109+
POSTGRES_CAS_PASS=POSTGRES_CAS_PASS
110+
POSTGRES_CUSTOMER_CAS_CONNECTION_NAME=POSTGRES_CUSTOMER_CAS_CONNECTION_NAME
111+
POSTGRES_CUSTOMER_CAS_PASS=POSTGRES_CUSTOMER_CAS_PASS
112+
POSTGRES_CUSTOMER_CAS_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_DOMAIN_NAME
113+
POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME=POSTGRES_CUSTOMER_CAS_INVALID_DOMAIN_NAME
114+
POSTGRES_MCP_CONNECTION_NAME=POSTGRES_MCP_CONNECTION_NAME
115+
POSTGRES_MCP_PASS=POSTGRES_MCP_PASS
116+
SQLSERVER_CONNECTION_NAME=SQLSERVER_CONNECTION_NAME
117+
SQLSERVER_USER=SQLSERVER_USER
118+
SQLSERVER_PASS=SQLSERVER_PASS
119+
SQLSERVER_DB=SQLSERVER_DB
120+
QUOTA_PROJECT=QUOTA_PROJECT
121+
)
122+
123+
if [[ -z "$TEST_PROJECT" ]] ; then
124+
echo "Set TEST_PROJECT environment variable to the project containing"
125+
echo "the e2e test suite secrets."
126+
exit 1
127+
fi
128+
129+
echo "Getting test secrets from $TEST_PROJECT into $1"
130+
{
131+
for env_name in "${secret_vars[@]}" ; do
132+
env_var_name="${env_name%%=*}"
133+
secret_name="${env_name##*=}"
134+
set -x
135+
val=$(gcloud secrets versions access latest --project "$TEST_PROJECT" --secret="$secret_name")
136+
echo "export $env_var_name='$val'"
137+
done
138+
} > "$1"
139+
140+
}
141+
142+
## help - prints the help details
143+
##
144+
function help() {
145+
# This will print the comments beginning with ## above each function
146+
# in this file.
147+
148+
echo "build.sh <command> <arguments>"
149+
echo
150+
echo "Commands to assist with local development and CI builds."
151+
echo
152+
echo "Commands:"
153+
echo
154+
grep -e '^##' "$SCRIPT_FILE" | sed -e 's/##/ /'
155+
}
156+
157+
set -euo pipefail
158+
159+
# Check CLI Arguments
160+
if [[ "$#" -lt 1 ]] ; then
161+
help
162+
exit 1
163+
fi
164+
165+
cd "$SCRIPT_DIR"
166+
167+
"$@"
168+

google/cloud/sql/connector/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
import aiohttp
2323
from cryptography.hazmat.backends import default_backend
2424
from cryptography.x509 import load_pem_x509_certificate
25-
2625
from google.auth.credentials import TokenState
2726
from google.auth.transport import requests
27+
2828
from google.cloud.sql.connector.connection_info import ConnectionInfo
2929
from google.cloud.sql.connector.connection_name import ConnectionName
3030
from google.cloud.sql.connector.exceptions import AutoIAMAuthNotSupported

google/cloud/sql/connector/connector.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import google.auth
2929
from google.auth.credentials import Credentials
3030
from google.auth.credentials import with_scopes_if_required
31+
3132
import google.cloud.sql.connector.asyncpg as asyncpg
3233
from google.cloud.sql.connector.client import CloudSQLClient
3334
from google.cloud.sql.connector.enums import DriverMapping

google/cloud/sql/connector/refresh_utils.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
from typing import Any, Callable
2525

2626
import aiohttp
27-
2827
from google.auth.credentials import Credentials
2928
from google.auth.credentials import Scoped
3029
import google.auth.transport.requests

noxfile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,15 @@ def format(session):
6969
*LINT_PATHS,
7070
)
7171

72+
@nox.session()
73+
def build(session):
74+
"""
75+
Just run the default tools to install requirements.
76+
"""
77+
# Install all test dependencies, then install this package in-place.
78+
session.install("-r", "requirements-test.txt")
79+
session.install("-e", ".")
80+
session.install("-r", "requirements.txt")
7281

7382
def default(session, path):
7483
# Install all test dependencies, then install this package in-place.

tests/unit/mocks.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@
3131
from cryptography.hazmat.primitives import serialization
3232
from cryptography.hazmat.primitives.asymmetric import rsa
3333
from cryptography.x509.oid import NameOID
34-
3534
from google.auth import _helpers
3635
from google.auth.credentials import Credentials
3736
from google.auth.credentials import TokenState
37+
3838
from google.cloud.sql.connector.connector import _DEFAULT_UNIVERSE_DOMAIN
3939
from google.cloud.sql.connector.utils import generate_keys
4040
from google.cloud.sql.connector.utils import write_to_file

tests/unit/test_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717

1818
from aiohttp import ClientResponseError
1919
from aioresponses import aioresponses
20+
from google.auth.credentials import Credentials
2021
from mocks import FakeCredentials
2122
import pytest
2223

23-
from google.auth.credentials import Credentials
2424
from google.cloud.sql.connector.client import CloudSQLClient
2525
from google.cloud.sql.connector.utils import generate_keys
2626
from google.cloud.sql.connector.version import __version__ as version

tests/unit/test_connector.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@
1919
from typing import Union
2020

2121
from aiohttp import ClientResponseError
22+
from google.auth.credentials import Credentials
2223
from mock import patch
2324
import pytest # noqa F401 Needed to run the tests
2425

25-
from google.auth.credentials import Credentials
2626
from google.cloud.sql.connector import Connector
2727
from google.cloud.sql.connector import create_async_connector
2828
from google.cloud.sql.connector import IPTypes

tests/unit/test_refresh_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@
2020
import datetime
2121

2222
from conftest import SCOPES # type: ignore
23+
import google.auth
24+
from google.auth.credentials import Credentials
25+
from google.auth.credentials import TokenState
26+
import google.oauth2.credentials
2327
from mock import Mock
2428
from mock import patch
2529
import pytest # noqa F401 Needed to run the tests
2630

27-
import google.auth
28-
from google.auth.credentials import Credentials
29-
from google.auth.credentials import TokenState
3031
from google.cloud.sql.connector.refresh_utils import _downscope_credentials
3132
from google.cloud.sql.connector.refresh_utils import _exponential_backoff
3233
from google.cloud.sql.connector.refresh_utils import _is_valid
3334
from google.cloud.sql.connector.refresh_utils import _seconds_until_refresh
3435
from google.cloud.sql.connector.refresh_utils import retry_50x
35-
import google.oauth2.credentials
3636

3737

3838
@pytest.fixture

0 commit comments

Comments
 (0)