Skip to content

Commit 1167fd4

Browse files
committed
more unit test
1 parent 40895b0 commit 1167fd4

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

provisioner/terraform/parse.go

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"fmt"
77
"os"
88
"path/filepath"
9+
"slices"
910
"sort"
1011
"strings"
1112

@@ -83,20 +84,26 @@ func (s *server) loadWorkspaceTags(ctx context.Context, module *tfconfig.Module)
8384
s.logger.Debug(ctx, "only .tf files can be parsed", "filename", dataResource.Pos.Filename)
8485
continue
8586
}
87+
// We know in which HCL file is the data resource defined.
8688
file, diags = parser.ParseHCLFile(dataResource.Pos.Filename)
8789

8890
if diags.HasErrors() {
8991
return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error())
9092
}
9193

92-
// Parse root to find "coder_workspace_tags"
94+
// Parse root to find "coder_workspace_tags".
9395
content, _, diags := file.Body.PartialContent(rootTemplateSchema)
9496
if diags.HasErrors() {
9597
return nil, xerrors.Errorf("can't parse the resource file: %s", diags.Error())
9698
}
9799

100+
// Iterate over blocks to locate the exact "coder_workspace_tags" data resource.
98101
for _, block := range content.Blocks {
99-
// Parse "coder_workspace_tags" to find all key-value tags
102+
if !slices.Equal(block.Labels, []string{"coder_workspace_tags", dataResource.Name}) {
103+
continue
104+
}
105+
106+
// Parse "coder_workspace_tags" to find all key-value tags.
100107
resContent, _, diags := block.Body.PartialContent(coderWorkspaceTagsSchema)
101108
if diags.HasErrors() {
102109
return nil, xerrors.Errorf(`can't parse the resource coder_workspace_tags: %s`, diags.Error())

provisioner/terraform/parse_test.go

+64
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,70 @@ func TestParse(t *testing.T) {
261261
},
262262
},
263263
},
264+
{
265+
Name: "workspace-tags-in-a-single-file",
266+
Files: map[string]string{
267+
"main.tf": `
268+
269+
data "coder_parameter" "os_selector" {
270+
name = "os_selector"
271+
display_name = "Operating System"
272+
mutable = false
273+
274+
default = "osx"
275+
276+
option {
277+
icon = "/icons/linux.png"
278+
name = "Linux"
279+
value = "linux"
280+
}
281+
option {
282+
icon = "/icons/osx.png"
283+
name = "OSX"
284+
value = "osx"
285+
}
286+
option {
287+
icon = "/icons/windows.png"
288+
name = "Windows"
289+
value = "windows"
290+
}
291+
}
292+
293+
data "coder_parameter" "feature_cache_enabled" {
294+
name = "feature_cache_enabled"
295+
display_name = "Enable cache?"
296+
type = "bool"
297+
298+
default = false
299+
}
300+
301+
data "coder_parameter" "feature_debug_enabled" {
302+
name = "feature_debug_enabled"
303+
display_name = "Enable debug?"
304+
type = "bool"
305+
306+
default = true
307+
}
308+
309+
data "coder_workspace_tags" "custom_workspace_tags" {
310+
tags = {
311+
"cluster" = "developers"
312+
"os" = data.coder_parameter.os_selector.value
313+
"debug" = "${data.coder_parameter.feature_debug_enabled.value}+12345"
314+
"cache" = data.coder_parameter.feature_cache_enabled.value == "true" ? "nix-with-cache" : "no-cache"
315+
}
316+
}
317+
`,
318+
},
319+
Response: &proto.ParseComplete{
320+
WorkspaceTags: map[string]string{
321+
"cluster": `"developers"`,
322+
"os": `data.coder_parameter.os_selector.value`,
323+
"debug": `"${data.coder_parameter.feature_debug_enabled.value}+12345"`,
324+
"cache": `data.coder_parameter.feature_cache_enabled.value == "true" ? "nix-with-cache" : "no-cache"`,
325+
},
326+
},
327+
},
264328
}
265329

266330
for _, testCase := range testCases {

0 commit comments

Comments
 (0)