@@ -42,13 +42,13 @@ func Test_Extract(t *testing.T) {
4242 failPreview bool
4343 input preview.Input
4444
45- expTags map [string ]string
46- unknownTags []string
47- params map [string ]assertParam
48- variables map [string ]assertVariable
49- presetsFuncs func (t * testing.T , presets []types.Preset )
50- presets map [string ]assertPreset
51- warnings []* regexp.Regexp
45+ expTags map [string ]string
46+ unknownTags []string
47+ params map [string ]assertParam
48+ variables map [string ]assertVariable
49+ presetsFuncs func (t * testing.T , presets []types.Preset )
50+ presets map [string ]assertPreset
51+ warnings []* regexp.Regexp
5252 secretRequirements []types.SecretRequirement
5353 }{
5454 {
@@ -1200,6 +1200,60 @@ data "coder_secret" "x" {
12001200` ,
12011201 wantDiag : `Exactly one of "env" or "file" must be set` ,
12021202 },
1203+ {
1204+ name : "env wrong type (number)" ,
1205+ tf : `
1206+ data "coder_secret" "x" {
1207+ env = 42
1208+ help_message = "ok"
1209+ }
1210+ ` ,
1211+ wantDiag : `Expected a string` ,
1212+ },
1213+ {
1214+ name : "file wrong type (bool)" ,
1215+ tf : `
1216+ data "coder_secret" "x" {
1217+ file = true
1218+ help_message = "ok"
1219+ }
1220+ ` ,
1221+ wantDiag : `Expected a string` ,
1222+ },
1223+ {
1224+ name : "env null" ,
1225+ tf : `
1226+ data "coder_secret" "x" {
1227+ env = null
1228+ help_message = "ok"
1229+ }
1230+ ` ,
1231+ wantDiag : `Expected a string` ,
1232+ },
1233+ {
1234+ name : "file null" ,
1235+ tf : `
1236+ data "coder_secret" "x" {
1237+ file = null
1238+ help_message = "ok"
1239+ }
1240+ ` ,
1241+ wantDiag : `Expected a string` ,
1242+ },
1243+ {
1244+ name : "duplicate block labels" ,
1245+ tf : `
1246+ data "coder_secret" "x" {
1247+ env = "A"
1248+ help_message = "first"
1249+ }
1250+ data "coder_secret" "x" {
1251+ env = "B"
1252+ help_message = "second"
1253+ }
1254+ ` ,
1255+ wantDiag : `duplicate coder_secret blocks with name "x"` ,
1256+ },
12031257 }
12041258 for _ , tc := range tests {
12051259 t .Run (tc .name , func (t * testing.T ) {
@@ -1219,3 +1273,42 @@ data "coder_secret" "x" {
12191273 })
12201274 }
12211275}
1276+
1277+ // Test_SecretRequirement_DuplicateDetectionRequiresExtractionSuccess pins the
1278+ // current behavior: duplicate-label detection only fires for blocks that
1279+ // successfully extract. When one of two same-labeled blocks has an extraction
1280+ // error (e.g. wrong attribute type), the dedup bookkeeping skips it, so the
1281+ // "duplicate coder_secret blocks" diagnostic does not appear — only the
1282+ // per-block extraction error does. This matches parameter.go's precedent.
1283+ // If this behavior is ever changed to "track duplicates regardless of
1284+ // extraction success," update this test.
1285+ func Test_SecretRequirement_DuplicateDetectionRequiresExtractionSuccess (t * testing.T ) {
1286+ t .Parallel ()
1287+ tf := `
1288+ data "coder_secret" "x" {
1289+ env = 42
1290+ help_message = "first"
1291+ }
1292+ data "coder_secret" "x" {
1293+ env = "B"
1294+ help_message = "second"
1295+ }
1296+ `
1297+ fsys := fstest.MapFS {"main.tf" : & fstest.MapFile {Data : []byte (tf )}}
1298+ _ , diags := preview .Preview (context .Background (), preview.Input {}, fsys )
1299+ require .True (t , diags .HasErrors (), "expected errors; got %v" , diags )
1300+
1301+ var hasTypeErr , hasDupeErr bool
1302+ for _ , d := range diags {
1303+ msg := d .Summary + " " + d .Detail
1304+ if strings .Contains (msg , "Expected a string" ) {
1305+ hasTypeErr = true
1306+ }
1307+ if strings .Contains (msg , "duplicate coder_secret blocks" ) {
1308+ hasDupeErr = true
1309+ }
1310+ }
1311+ require .True (t , hasTypeErr , "expected type error for env=42; got: %v" , diags )
1312+ require .False (t , hasDupeErr ,
1313+ "duplicate diagnostic unexpectedly fired; dedup should only track successful extractions. diags: %v" , diags )
1314+ }
0 commit comments