@@ -2,7 +2,7 @@ use crate::common::config::cloudflare_config_json;
22use crate :: common:: runtime:: {
33 RuntimeEnvironment , RuntimeProcess , RuntimeProcessHandle , TestError , TestResult , origin_port,
44} ;
5- use error_stack:: ResultExt as _;
5+ use error_stack:: { Report , ResultExt as _} ;
66use std:: io:: { BufRead as _, BufReader } ;
77use std:: path:: { Path , PathBuf } ;
88use std:: process:: { Child , Command , Stdio } ;
@@ -25,6 +25,7 @@ pub struct CloudflareWorkers;
2525const CLOUDFLARE_DEFAULT_PORT : u16 = 8787 ;
2626const CI_CONFIG_TEMPLATE : & str = "wrangler.ci.toml" ;
2727const GENERATED_CI_CONFIG : & str = "wrangler.integration.generated.toml" ;
28+ const TRUSTED_SERVER_CONFIG_PLACEHOLDER : & str = "TRUSTED_SERVER_CONFIG = \" {}\" " ;
2829
2930fn write_generated_ci_config ( wrangler_dir : & Path ) -> TestResult < String > {
3031 let template_path = wrangler_dir. join ( CI_CONFIG_TEMPLATE ) ;
@@ -35,10 +36,7 @@ fn write_generated_ci_config(wrangler_dir: &Path) -> TestResult<String> {
3536 template_path. display( )
3637 ) ) ?;
3738 let config_json = cloudflare_config_json ( origin_port ( ) ) ?;
38- let generated = template. replace (
39- "TRUSTED_SERVER_CONFIG = \" {}\" " ,
40- & format ! ( "TRUSTED_SERVER_CONFIG = '''{config_json}'''" ) ,
41- ) ;
39+ let generated = inject_cloudflare_config ( & template, & config_json) ?;
4240 let output_path = wrangler_dir. join ( GENERATED_CI_CONFIG ) ;
4341 std:: fs:: write ( & output_path, generated)
4442 . change_context ( TestError :: RuntimeSpawn )
@@ -49,6 +47,20 @@ fn write_generated_ci_config(wrangler_dir: &Path) -> TestResult<String> {
4947 Ok ( GENERATED_CI_CONFIG . to_string ( ) )
5048}
5149
50+ fn inject_cloudflare_config ( template : & str , config_json : & str ) -> TestResult < String > {
51+ let placeholder_count = template. matches ( TRUSTED_SERVER_CONFIG_PLACEHOLDER ) . count ( ) ;
52+ if placeholder_count != 1 {
53+ return Err ( Report :: new ( TestError :: RuntimeSpawn ) . attach ( format ! (
54+ "Cloudflare CI wrangler config must contain exactly one `{TRUSTED_SERVER_CONFIG_PLACEHOLDER}` placeholder, found {placeholder_count}"
55+ ) ) ) ;
56+ }
57+
58+ Ok ( template. replace (
59+ TRUSTED_SERVER_CONFIG_PLACEHOLDER ,
60+ & format ! ( "TRUSTED_SERVER_CONFIG = '''{config_json}'''" ) ,
61+ ) )
62+ }
63+
5264impl RuntimeEnvironment for CloudflareWorkers {
5365 fn id ( & self ) -> & ' static str {
5466 "cloudflare"
@@ -182,3 +194,43 @@ impl Drop for CloudflareHandle {
182194 let _ = self . child . wait ( ) ;
183195 }
184196}
197+
198+ #[ cfg( test) ]
199+ mod tests {
200+ use super :: * ;
201+
202+ #[ test]
203+ fn inject_cloudflare_config_replaces_single_placeholder ( ) {
204+ let template = format ! ( "[vars]\n {TRUSTED_SERVER_CONFIG_PLACEHOLDER}\n " ) ;
205+
206+ let generated = inject_cloudflare_config ( & template, r#"{"app_config":"blob"}"# )
207+ . expect ( "should inject Cloudflare config" ) ;
208+
209+ assert ! (
210+ generated. contains( "TRUSTED_SERVER_CONFIG = '''{\" app_config\" :\" blob\" }'''" ) ,
211+ "should inject generated config JSON"
212+ ) ;
213+ assert ! (
214+ !generated. contains( TRUSTED_SERVER_CONFIG_PLACEHOLDER ) ,
215+ "should remove placeholder"
216+ ) ;
217+ }
218+
219+ #[ test]
220+ fn inject_cloudflare_config_rejects_missing_placeholder ( ) {
221+ let result = inject_cloudflare_config ( "[vars]\n " , r#"{"app_config":"blob"}"# ) ;
222+
223+ assert ! ( result. is_err( ) , "should reject missing placeholder" ) ;
224+ }
225+
226+ #[ test]
227+ fn inject_cloudflare_config_rejects_duplicate_placeholders ( ) {
228+ let template = format ! (
229+ "[vars]\n {TRUSTED_SERVER_CONFIG_PLACEHOLDER}\n {TRUSTED_SERVER_CONFIG_PLACEHOLDER}\n "
230+ ) ;
231+
232+ let result = inject_cloudflare_config ( & template, r#"{"app_config":"blob"}"# ) ;
233+
234+ assert ! ( result. is_err( ) , "should reject duplicate placeholders" ) ;
235+ }
236+ }
0 commit comments