11from __future__ import annotations
22
3+ import hashlib
34import os
45import pathlib
56import typing
67
78import nox
9+ import yaml
810from nox import options
911
1012if typing .TYPE_CHECKING :
1113 import collections .abc
1214
13- PATH_TO_PROJECT = pathlib .Path (__name__ ).parent
15+ PATH_TO_PROJECT = pathlib .Path (__name__ ).parent . absolute ()
1416SCRIPT_PATHS = ["noxfile.py" , PATH_TO_PROJECT / "scripts" , PATH_TO_PROJECT / "test" ]
17+ TESTS_PATH = PATH_TO_PROJECT / "test"
1518
1619DRIVER_PATHS = {
17- "asyncpg" : PATH_TO_PROJECT / "test" / "driver_asyncpg" ,
18- "aiosqlite" : PATH_TO_PROJECT / "test" / "driver_aiosqlite" ,
19- "sqlite3" : PATH_TO_PROJECT / "test" / "driver_sqlite3" ,
20+ "asyncpg" : TESTS_PATH / "driver_asyncpg" ,
21+ "aiosqlite" : TESTS_PATH / "driver_aiosqlite" ,
22+ "sqlite3" : TESTS_PATH / "driver_sqlite3" ,
2023}
2124
2225SQLC_CONFIGS = ["sqlc.yaml" ]
@@ -84,6 +87,49 @@ def sqlc_check(session: nox.Session, driver: str) -> None:
8487
8588
8689@nox .session (reuse_venv = True )
90+ def update_test_plugin (session : nox .Session ) -> None :
91+ # Build the plugin
92+ wasm_path = TESTS_PATH / "sqlc-gen-better-python.wasm"
93+
94+ with session .chdir ("plugin" ):
95+ session .run (
96+ "go" ,
97+ "build" ,
98+ "-o" ,
99+ str (wasm_path ),
100+ env = {"GOOS" : "wasip1" , "GOARCH" : "wasm" },
101+ external = True ,
102+ )
103+
104+ # Calculate the SHA256 hash
105+ sha256_hasher = hashlib .sha256 ()
106+ with wasm_path .open ("rb" ) as fp :
107+ while True :
108+ data = fp .read (65536 ) # 64kb chunks
109+ if not data :
110+ break
111+
112+ sha256_hasher .update (data )
113+
114+ plugin_hash = sha256_hasher .hexdigest ()
115+
116+ # Update the SHA256 in the config files
117+ for driver_name , driver_path in DRIVER_PATHS .items ():
118+ for config_filename in SQLC_CONFIGS :
119+ config_path = driver_path / config_filename
120+
121+ with config_path .open () as fp :
122+ config = yaml .safe_load (fp )
123+
124+ config ["plugins" ][0 ]["wasm" ]["sha256" ] = plugin_hash
125+
126+ with config_path .open ("w" ) as fp :
127+ yaml .safe_dump (config , fp )
128+
129+ sqlc_generate (session , driver_name )
130+
131+
132+ @nox .session (reuse_venv = True , requires = ["update_test_plugin" ])
87133def sqlite3 (session : nox .Session ) -> None :
88134 uv_sync (session , include_self = True , groups = ["pyright" , "ruff" ])
89135
@@ -92,7 +138,7 @@ def sqlite3(session: nox.Session) -> None:
92138 session .run ("ruff" , "check" , * session .posargs , DRIVER_PATHS ["sqlite3" ])
93139
94140
95- @nox .session (reuse_venv = True )
141+ @nox .session (reuse_venv = True , requires = [ "update_test_plugin" ] )
96142def sqlite3_check (session : nox .Session ) -> None :
97143 uv_sync (session , include_self = True , groups = ["pyright" , "ruff" ])
98144
@@ -101,7 +147,7 @@ def sqlite3_check(session: nox.Session) -> None:
101147 session .run ("ruff" , "check" , * session .posargs , DRIVER_PATHS ["sqlite3" ])
102148
103149
104- @nox .session (reuse_venv = True )
150+ @nox .session (reuse_venv = True , requires = [ "update_test_plugin" ] )
105151def aiosqlite (session : nox .Session ) -> None :
106152 uv_sync (session , include_self = True , groups = ["pyright" , "ruff" ])
107153
0 commit comments