@@ -9,17 +9,17 @@ import (
99)
1010
1111/*
12- TestLoadMissingFile ensures Load correctly returns an error when the config file does not exist .
12+ TestLoadMissingFile ensures that loading a non-existent config file returns an error .
1313*/
1414func TestLoadMissingFile (t * testing.T ) {
15- _ , err := Load ("nonexistent_config .json" )
15+ _ , err := Load ("nonexistent .json" )
1616 if err == nil {
17- t .Fatalf ("Expected error for missing config file, got nil" )
17+ t .Fatalf ("Expected error for missing config file, but got nil" )
1818 }
1919}
2020
2121/*
22- TestLoadUnreadableFile ensures Load correctly returns an error when the config file is unreadable .
22+ TestLoadUnreadableFile ensures that loading an unreadable file returns an error .
2323*/
2424func TestLoadUnreadableFile (t * testing.T ) {
2525 tmpFile , err := os .CreateTemp ("" , "unreadable_config_*.json" )
@@ -28,71 +28,65 @@ func TestLoadUnreadableFile(t *testing.T) {
2828 }
2929 defer os .Remove (tmpFile .Name ())
3030
31- if err := os .Chmod (tmpFile .Name (), 0000 ); err != nil {
32- t .Fatalf ("Failed to set file permissions: %v" , err )
33- }
31+ // Make the file unreadable
32+ os .Chmod (tmpFile .Name (), 0000 )
3433 defer os .Chmod (tmpFile .Name (), 0644 ) // Restore permissions after test
3534
3635 _ , err = Load (tmpFile .Name ())
3736 if err == nil {
38- t .Fatalf ("Expected error for unreadable file, got nil" )
37+ t .Fatalf ("Expected error for unreadable file, but got nil" )
3938 }
4039}
4140
4241/*
43- TestLoadInvalidJSONFormat ensures Load correctly returns an error for malformed JSON.
42+ TestLoadInvalidJSONFormat ensures that loading a file with invalid JSON format returns an error .
4443*/
4544func TestLoadInvalidJSONFormat (t * testing.T ) {
46- tmpFile , err := os .CreateTemp ("" , "invalid_json_ *.json" )
45+ tmpFile , err := os .CreateTemp ("" , "invalid_config_ *.json" )
4746 if err != nil {
4847 t .Fatalf ("Failed to create temp file: %v" , err )
4948 }
5049 defer os .Remove (tmpFile .Name ())
5150
52- // Invalid JSON (missing closing brace)
53- invalidJSON := `{"url": {"base": "http://example.org"`
51+ invalidJSON := `{"url": {"base": "http://example.org"` // Missing closing brace
5452 if _ , err := tmpFile .Write ([]byte (invalidJSON )); err != nil {
5553 t .Fatalf ("Failed to write to temp file: %v" , err )
5654 }
5755 tmpFile .Close ()
5856
5957 _ , err = Load (tmpFile .Name ())
6058 if err == nil {
61- t .Fatalf ("Expected error for malformed JSON, got nil" )
59+ t .Fatalf ("Expected error for invalid JSON format, but got nil" )
6260 }
6361}
6462
6563/*
66- TestLoadVerboseMode ensures that verbose mode triggers PrintNonEmptyFields .
64+ TestLoadVerboseMode ensures that verbose mode prints additional output .
6765*/
6866func TestLoadVerboseMode (t * testing.T ) {
6967 Verbose = true
7068 defer func () { Verbose = false }()
7169
72- tmpFile , err := os .CreateTemp ("" , "verbose_config_ *.json" )
70+ tmpFile , err := os .CreateTemp ("" , "valid_config_ *.json" )
7371 if err != nil {
7472 t .Fatalf ("Failed to create temp file: %v" , err )
7573 }
7674 defer os .Remove (tmpFile .Name ())
7775
78- validJSON := `{"url": {"base": "http://example.org"}}`
76+ validJSON := `{"url": {"base": "http://example.org", "routes": ["/test"], "includeBase": true }}`
7977 if _ , err := tmpFile .Write ([]byte (validJSON )); err != nil {
8078 t .Fatalf ("Failed to write to temp file: %v" , err )
8179 }
8280 tmpFile .Close ()
8381
84- cfg , err : = Load (tmpFile .Name ())
82+ _ , err = Load (tmpFile .Name ())
8583 if err != nil {
86- t .Fatalf ("Expected valid config, got error: %v" , err )
87- }
88-
89- if cfg .URL .Base != "http://example.org" {
90- t .Errorf ("Expected Base URL 'http://example.org', got '%s'" , cfg .URL .Base )
84+ t .Fatalf ("Expected successful load with verbose mode, but got error: %v" , err )
9185 }
9286}
9387
9488/*
95- TestOverrideWithInvalidField ensures that OverrideWithCLI skips invalid fields safely .
89+ TestOverrideWithInvalidField ensures that OverrideWithCLI correctly skips invalid fields.
9690*/
9791func TestOverrideWithInvalidField (t * testing.T ) {
9892 cfg := & Config {}
@@ -101,9 +95,8 @@ func TestOverrideWithInvalidField(t *testing.T) {
10195 overrides := Config {}
10296 field := reflect .ValueOf (& overrides ).Elem ().FieldByName ("InvalidField" )
10397
104- // Ensure the field exists and can be modified
10598 if field .IsValid () && field .CanSet () {
106- field .Set (reflect .ValueOf (42 )) // Invalid field
99+ field .Set (reflect .ValueOf (42 ))
107100 }
108101
109102 defer func () {
@@ -116,64 +109,7 @@ func TestOverrideWithInvalidField(t *testing.T) {
116109}
117110
118111/*
119- TestOverrideWithInvalidNestedField ensures that OverrideWithCLI skips invalid nested fields safely.
120- */
121- func TestOverrideWithInvalidNestedField (t * testing.T ) {
122- cfg := & Config {}
123- cfg .ApplyDefaults ()
124-
125- overrides := Config {}
126-
127- // Get the field reference
128- field := reflect .ValueOf (& overrides ).Elem ().FieldByName ("URL" )
129-
130- // Ensure the field exists and is of type struct before attempting an invalid override
131- if field .IsValid () && field .Kind () == reflect .Struct {
132- defer func () {
133- if r := recover (); r != nil {
134- t .Fatalf ("Expected safe handling of invalid nested fields, but got panic: %v" , r )
135- }
136- }()
137-
138- // Try setting an invalid value (int) but ensure it's prevented before setting
139- invalidValue := reflect .ValueOf (42 )
140- if field .CanSet () && field .Kind () == invalidValue .Kind () { // Ensure types match
141- field .Set (invalidValue ) // This should NOT execute due to validation.
142- }
143- }
144-
145- // Ensure the override function does not crash on incorrect input
146- cfg .OverrideWithCLI (overrides )
147- }
148-
149- /*
150- TestOverrideWithValidSubField ensures that OverrideWithCLI correctly overrides valid subfields.
151- */
152- func TestOverrideWithValidSubField (t * testing.T ) {
153- cfg := & Config {}
154- cfg .ApplyDefaults ()
155-
156- overrides := Config {
157- ParseRules : struct {
158- Title string `json:"title,omitempty"`
159- MetaDescription string `json:"metaDescription,omitempty"`
160- ArticleContent string `json:"articleContent,omitempty"`
161- Author string `json:"author,omitempty"`
162- DatePublished string `json:"datePublished,omitempty"`
163- }{
164- Title : "New Title" ,
165- },
166- }
167-
168- cfg .OverrideWithCLI (overrides )
169-
170- if cfg .ParseRules .Title != "New Title" {
171- t .Errorf ("Expected Title to be 'New Title', got '%s'" , cfg .ParseRules .Title )
172- }
173- }
174-
175- /*
176- TestOverrideWithEmptySlices ensures that OverrideWithCLI skips empty slice values.
112+ TestOverrideWithEmptySlices ensures that OverrideWithCLI skips overriding fields with empty slices.
177113*/
178114func TestOverrideWithEmptySlices (t * testing.T ) {
179115 cfg := & Config {}
@@ -192,12 +128,12 @@ func TestOverrideWithEmptySlices(t *testing.T) {
192128 cfg .OverrideWithCLI (overrides )
193129
194130 if len (cfg .URL .Routes ) == 0 {
195- t .Errorf ("Expected Routes to remain unchanged, but they were overridden with an empty slice." )
131+ t .Errorf ("Expected routes to remain unchanged, but they were overridden with an empty slice." )
196132 }
197133}
198134
199135/*
200- TestOverrideWithValidFields ensures that OverrideWithCLI correctly applies non-zero overrides.
136+ TestOverrideWithValidFields ensures that OverrideWithCLI correctly applies valid overrides.
201137*/
202138func TestOverrideWithValidFields (t * testing.T ) {
203139 cfg := & Config {}
@@ -210,18 +146,18 @@ func TestOverrideWithValidFields(t *testing.T) {
210146 RetryAttempts int `json:"retryAttempts"`
211147 UserAgent string `json:"userAgent"`
212148 }{
213- MaxDepth : 5 ,
214- RateLimit : 2 .5 ,
149+ MaxDepth : 10 ,
150+ RateLimit : 3 .5 ,
215151 },
216152 }
217153
218154 cfg .OverrideWithCLI (overrides )
219155
220- if cfg .ScrapingOptions .MaxDepth != 5 {
221- t .Errorf ("Expected MaxDepth to be overridden to 5 , got '%d' " , cfg .ScrapingOptions .MaxDepth )
156+ if cfg .ScrapingOptions .MaxDepth != 10 {
157+ t .Errorf ("Expected MaxDepth to be overridden to 10 , got %d " , cfg .ScrapingOptions .MaxDepth )
222158 }
223159
224- if cfg .ScrapingOptions .RateLimit != 2 .5 {
225- t .Errorf ("Expected RateLimit to be overridden to 2 .5, got '%f' " , cfg .ScrapingOptions .RateLimit )
160+ if cfg .ScrapingOptions .RateLimit != 3 .5 {
161+ t .Errorf ("Expected RateLimit to be overridden to 3 .5, got %f " , cfg .ScrapingOptions .RateLimit )
226162 }
227163}
0 commit comments