@@ -15,7 +15,7 @@ It sets the working directory to the project root (two levels up from cmd/scrape
1515and returns the combined output along with any error.
1616
1717Parameters:
18- - t: The current testing context (not used directly, but conforms to typical test helper function signatures) .
18+ - t: The current testing context.
1919 - args: A variadic list of arguments to be passed to the go run command.
2020
2121Usage:
@@ -31,39 +31,39 @@ func runMainCommand(_ *testing.T, args ...string) (string, error) {
3131
3232/*
3333TestFlagRegistration verifies that all necessary command-line flags are properly registered.
34- The application depends on these flags for configuration input and URL overrides.
35-
36- Checks:
37- - "config" and "c" flags
38- - "url" flag
34+ The application depends on these flags for configuration input and CLI overrides.
3935*/
4036func TestFlagRegistration (t * testing.T ) {
41- if f := flag .Lookup ("config" ); f == nil {
42- t .Error ("Expected flag 'config' to be registered" )
43- }
44- if f := flag .Lookup ("c" ); f == nil {
45- t .Error ("Expected shorthand flag 'c' to be registered" )
46- }
47- if f := flag .Lookup ("url" ); f == nil {
48- t .Error ("Expected flag 'url' to be registered" )
37+ expectedFlags := []string {"config" , "c" , "url" , "maxDepth" , "rateLimit" }
38+ for _ , flagName := range expectedFlags {
39+ if f := flag .Lookup (flagName ); f == nil {
40+ t .Errorf ("Expected flag '%s' to be registered" , flagName )
41+ }
4942 }
5043}
5144
5245/*
53- TestMainExecution runs the main program with a valid configuration file and checks for the expected output.
46+ TestMainExecution runs the main program with a valid configuration file
47+ and ensures it initializes correctly.
5448*/
5549func TestMainExecution (t * testing.T ) {
56- output , err := runMainCommand (t , "--config" , "configs/default.json" )
50+ output , err := runMainCommand (t )
5751 if err != nil {
5852 t .Fatalf ("Failed to run main.go: %v\n Output: %s" , err , output )
5953 }
6054
61- if ! strings .Contains (output , "Welcome to Scrapey CLI!" ) {
62- t .Errorf ("Expected welcome message not found in output.\n Output: %s" , output )
55+ // Define expected phrases used multiple times
56+ requiredPhrases := []string {
57+ "Welcome to Scrapey CLI!" ,
58+ "Scrapey CLI initialization complete." ,
59+ "Base URL: https://example.com" ,
6360 }
6461
65- if ! strings .Contains (output , "Base URL: https://example.com" ) {
66- t .Errorf ("Expected base URL output not found.\n Output: %s" , output )
62+ // Validate presence of required phrases
63+ for _ , phrase := range requiredPhrases {
64+ if ! strings .Contains (output , phrase ) {
65+ t .Errorf ("Expected output to contain '%s'.\n Output: %s" , phrase , output )
66+ }
6767 }
6868}
6969
@@ -77,6 +77,7 @@ func TestMainConfigFailure(t *testing.T) {
7777 t .Fatalf ("Expected failure due to config load error, but got success" )
7878 }
7979
80+ // Validate correct exit behavior
8081 if exitErr , ok := err .(* exec.ExitError ); ok {
8182 if exitErr .ExitCode () != 1 {
8283 t .Errorf ("Expected exit code 1, got %d" , exitErr .ExitCode ())
@@ -87,17 +88,42 @@ func TestMainConfigFailure(t *testing.T) {
8788}
8889
8990/*
90- TestURLOverride verifies that specifying a URL via CLI correctly overrides the Base URL.
91+ TestCLIOverrides verifies that CLI arguments correctly override the configuration.
92+
93+ It ensures that:
94+ - The base URL can be overridden.
95+ - Scraping depth (maxDepth) can be overridden.
96+ - Rate limit can be overridden.
97+
98+ The test **does not rely on exact print statements** to avoid fragility.
9199*/
92- func TestURLOverride (t * testing.T ) {
93- output , err := runMainCommand (t , "--config" , "configs/default.json" , "--url" , "https://example.org" )
100+ func TestCLIOverrides (t * testing.T ) {
101+ // CLI argument values (used multiple times)
102+ newBaseURL := "https://cli-example.com"
103+ newMaxDepth := "10"
104+ newRateLimit := "2.5"
105+
106+ // Run command
107+ output , err := runMainCommand (t ,
108+ "--url" , newBaseURL ,
109+ "--maxDepth" , newMaxDepth ,
110+ "--rateLimit" , newRateLimit ,
111+ )
94112 if err != nil {
95- t .Fatalf ("Failed to run main.go with URL override : %v\n Output: %s" , err , output )
113+ t .Fatalf ("Failed to run main.go with CLI overrides : %v\n Output: %s" , err , output )
96114 }
97- if ! strings .Contains (output , "Overriding config with URL flag:" ) {
98- t .Errorf ("Expected URL override message not found in output.\n Output: %s" , output )
115+
116+ // Expected CLI override outputs (used multiple times)
117+ expectedOutputs := map [string ]string {
118+ "Base URL: " : newBaseURL ,
119+ "ScrapingOptions.MaxDepth: " : newMaxDepth ,
120+ "ScrapingOptions.RateLimit: " : newRateLimit ,
99121 }
100- if ! strings .Contains (output , "Base URL: https://example.org" ) {
101- t .Errorf ("Expected overridden URL not found in output.\n Output: %s" , output )
122+
123+ // Validate overrides dynamically
124+ for key , expected := range expectedOutputs {
125+ if ! strings .Contains (output , key + expected ) {
126+ t .Errorf ("Expected override '%s%s' not found in output.\n Output: %s" , key , expected , output )
127+ }
102128 }
103129}
0 commit comments