@@ -2,12 +2,42 @@ package main
22
33import (
44 _ "embed"
5+ "strings"
56 "testing"
67)
78
89//go:embed testSamples/sampleReadmeBody.md
910var testBody string
1011
12+ // Test bodies extracted for better readability
13+ var (
14+ validModuleBody = `# Test Module
15+
16+ ` + "```tf\n " + `module "test-module" {
17+ source = "registry.coder.com/test-namespace/test-module/coder"
18+ version = "1.0.0"
19+ agent_id = coder_agent.example.id
20+ }
21+ ` + "```\n "
22+
23+ wrongNamespaceBody = `# Test Module
24+
25+ ` + "```tf\n " + `module "test-module" {
26+ source = "registry.coder.com/wrong-namespace/test-module/coder"
27+ version = "1.0.0"
28+ agent_id = coder_agent.example.id
29+ }
30+ ` + "```\n "
31+
32+ missingSourceBody = `# Test Module
33+
34+ ` + "```tf\n " + `module "test-module" {
35+ version = "1.0.0"
36+ agent_id = coder_agent.example.id
37+ }
38+ ` + "```\n "
39+ )
40+
1141func TestValidateCoderResourceReadmeBody (t * testing.T ) {
1242 t .Parallel ()
1343
@@ -27,9 +57,14 @@ func TestValidateModuleSourceURL(t *testing.T) {
2757 t .Run ("Valid source URL format" , func (t * testing.T ) {
2858 t .Parallel ()
2959
30- body := "# Test Module\n \n ```tf\n module \" test-module\" {\n source = \" registry.coder.com/test-namespace/test-module/coder\" \n version = \" 1.0.0\" \n agent_id = coder_agent.example.id\n }\n ```\n "
31- filePath := "registry/test-namespace/modules/test-module/README.md"
32- errs := validateModuleSourceURL (body , filePath )
60+ rm := coderResourceReadme {
61+ resourceType : "modules" ,
62+ filePath : "registry/test-namespace/modules/test-module/README.md" ,
63+ namespace : "test-namespace" ,
64+ resourceName : "test-module" ,
65+ body : validModuleBody ,
66+ }
67+ errs := validateModuleSourceURL (rm )
3368 if len (errs ) != 0 {
3469 t .Errorf ("Expected no errors, got: %v" , errs )
3570 }
@@ -38,68 +73,57 @@ func TestValidateModuleSourceURL(t *testing.T) {
3873 t .Run ("Invalid source URL format - wrong namespace" , func (t * testing.T ) {
3974 t .Parallel ()
4075
41- body := "# Test Module\n \n ```tf\n module \" test-module\" {\n source = \" registry.coder.com/wrong-namespace/test-module/coder\" \n version = \" 1.0.0\" \n agent_id = coder_agent.example.id\n }\n ```\n "
42- filePath := "registry/test-namespace/modules/test-module/README.md"
43- errs := validateModuleSourceURL (body , filePath )
76+ rm := coderResourceReadme {
77+ resourceType : "modules" ,
78+ filePath : "registry/test-namespace/modules/test-module/README.md" ,
79+ namespace : "test-namespace" ,
80+ resourceName : "test-module" ,
81+ body : wrongNamespaceBody ,
82+ }
83+ errs := validateModuleSourceURL (rm )
4484 if len (errs ) != 1 {
4585 t .Errorf ("Expected 1 error, got %d: %v" , len (errs ), errs )
4686 }
47- if len ( errs ) > 0 && ! contains (errs [0 ].Error (), "incorrect source URL format" ) {
87+ if ! strings . Contains (errs [0 ].Error (), "incorrect source URL format" ) {
4888 t .Errorf ("Expected source URL format error, got: %s" , errs [0 ].Error ())
4989 }
5090 })
5191
5292 t .Run ("Missing source URL" , func (t * testing.T ) {
5393 t .Parallel ()
5494
55- body := "# Test Module\n \n ```tf\n module \" other-module\" {\n source = \" registry.coder.com/other/other-module/coder\" \n version = \" 1.0.0\" \n agent_id = coder_agent.example.id\n }\n ```\n "
56- filePath := "registry/test-namespace/modules/test-module/README.md"
57- errs := validateModuleSourceURL (body , filePath )
95+ rm := coderResourceReadme {
96+ resourceType : "modules" ,
97+ filePath : "registry/test-namespace/modules/test-module/README.md" ,
98+ namespace : "test-namespace" ,
99+ resourceName : "test-module" ,
100+ body : missingSourceBody ,
101+ }
102+ errs := validateModuleSourceURL (rm )
58103 if len (errs ) != 1 {
59104 t .Errorf ("Expected 1 error, got %d: %v" , len (errs ), errs )
60105 }
61- if len ( errs ) > 0 && ! contains (errs [0 ].Error (), "did not find correct source URL" ) {
106+ if ! strings . Contains (errs [0 ].Error (), "did not find correct source URL" ) {
62107 t .Errorf ("Expected missing source URL error, got: %s" , errs [0 ].Error ())
63108 }
64109 })
65110
66- t .Run ("Module name with hyphens vs underscores" , func (t * testing.T ) {
67- t .Parallel ()
68-
69- body := "# Test Module\n \n ```tf\n module \" test_module\" {\n source = \" registry.coder.com/test-namespace/test-module/coder\" \n version = \" 1.0.0\" \n agent_id = coder_agent.example.id\n }\n ```\n "
70- filePath := "registry/test-namespace/modules/test-module/README.md"
71- errs := validateModuleSourceURL (body , filePath )
72- if len (errs ) != 0 {
73- t .Errorf ("Expected no errors for hyphen/underscore variation, got: %v" , errs )
74- }
75- })
76-
77111 t .Run ("Invalid file path format" , func (t * testing.T ) {
78112 t .Parallel ()
79113
80- body := "# Test Module"
81- filePath := "invalid/path/format"
82- errs := validateModuleSourceURL (body , filePath )
114+ rm := coderResourceReadme {
115+ resourceType : "modules" ,
116+ filePath : "invalid/path/format" ,
117+ namespace : "" , // Empty because path parsing failed
118+ resourceName : "" , // Empty because path parsing failed
119+ body : "# Test Module" ,
120+ }
121+ errs := validateModuleSourceURL (rm )
83122 if len (errs ) != 1 {
84123 t .Errorf ("Expected 1 error, got %d: %v" , len (errs ), errs )
85124 }
86- if len ( errs ) > 0 && ! contains (errs [0 ].Error (), "invalid module path format" ) {
125+ if ! strings . Contains (errs [0 ].Error (), "invalid module path format" ) {
87126 t .Errorf ("Expected path format error, got: %s" , errs [0 ].Error ())
88127 }
89128 })
90- }
91-
92- func contains (s , substr string ) bool {
93- return len (s ) >= len (substr ) && (s == substr || (len (s ) > len (substr ) &&
94- (s [:len (substr )] == substr || s [len (s )- len (substr ):] == substr ||
95- indexOfSubstring (s , substr ) >= 0 )))
96- }
97-
98- func indexOfSubstring (s , substr string ) int {
99- for i := 0 ; i <= len (s )- len (substr ); i ++ {
100- if s [i :i + len (substr )] == substr {
101- return i
102- }
103- }
104- return - 1
105- }
129+ }
0 commit comments