Skip to content

Commit a5fe7e9

Browse files
feat: add unit test for commands (#4)
- Add test for root, tc, and eBay commands with the separate test for flags - Add GH workflow to automate testing on PR and Push
1 parent a7a52b7 commit a5fe7e9

File tree

5 files changed

+204
-1
lines changed

5 files changed

+204
-1
lines changed

.github/workflows/unit-test.yml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
name: Unit Tests
2+
3+
on:
4+
workflow_dispatch:
5+
push:
6+
branches: ["main"]
7+
pull_request:
8+
9+
jobs:
10+
build-format:
11+
name: Build and Format
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout Code
15+
uses: actions/checkout@v3
16+
17+
- name: Setup Go
18+
uses: actions/setup-go@v4
19+
with:
20+
go-version: 1.19
21+
22+
- name: Clear the test cache
23+
run: go clean -testcache
24+
25+
- name: Install Executable to $GOPATH/bin
26+
run: go install
27+
28+
- name: Run Tests
29+
run: go test -v ./tests/...

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111

1212
var rootCmd = &cobra.Command{
1313
Use: "scrapy [command] [flags]",
14-
Short: "Scape the web from the command line",
14+
Short: "Scrape the web from the command line",
1515
Run: func(cmd *cobra.Command, args []string) {
1616
err := cmd.Help()
1717
if err != nil {

tests/ebay_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package test
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
// TestEbayCmd tests the ebay command
10+
func TestEbayCmd(t *testing.T) {
11+
12+
expectedOutput := "Searching for: iphone"
13+
cmd := exec.Command("scrapy", "ebay", "iphone")
14+
15+
// Capture the output
16+
output, err := cmd.CombinedOutput()
17+
if err != nil {
18+
t.Errorf("expected no error, but got: %v", err)
19+
}
20+
21+
// Validate the cli output
22+
got := strings.TrimSpace(string(output)[:22])
23+
if got != strings.Trim(expectedOutput, " ") {
24+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
25+
}
26+
27+
}
28+
29+
// TestEbayCmdPagesFlag tests the ebay command with the pages flag
30+
func TestEbayCmdPagesFlag(t *testing.T) {
31+
32+
expectedOutput := "Searching for: macbook"
33+
stringContains := "Scraping page 1 of 2"
34+
cmd := exec.Command("scrapy", "ebay", "macbook", "--pages", "2")
35+
36+
// Capture the output
37+
output, err := cmd.CombinedOutput()
38+
if err != nil {
39+
t.Errorf("expected no error, but got: %v", err)
40+
}
41+
42+
// Validate the 1st line of the cli output
43+
got := strings.TrimSpace(string(output)[:22])
44+
if got != strings.Trim(expectedOutput, " ") {
45+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
46+
}
47+
48+
// Validate the 2nd line of the cli output
49+
if strings.Contains(string(output), stringContains) != true {
50+
t.Errorf("expected %v, but got: %v", stringContains, string(output))
51+
}
52+
53+
}

tests/root_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package test
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
// TestRootCmd tests the root command (scrapy)
10+
func TestRootCmd(t *testing.T) {
11+
12+
expectedOutput := "Scrape the web from the command line"
13+
cmd := exec.Command("scrapy")
14+
15+
// Capture the output
16+
output, err := cmd.CombinedOutput()
17+
if err != nil {
18+
t.Errorf("expected no error, but got: %v", err)
19+
}
20+
21+
// Validate the cli output
22+
got := strings.TrimSpace(string(output)[:36])
23+
if got != expectedOutput {
24+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
25+
}
26+
27+
}
28+
29+
// TestRootCmdHelpFlag tests the root command (scrapy) with the help flag
30+
func TestRootCmdHelpFlag(t *testing.T) {
31+
32+
expectedOutput := "Scrape the web from the command line"
33+
cmd := exec.Command("scrapy", "--help")
34+
35+
// Capture the output
36+
output, err := cmd.CombinedOutput()
37+
if err != nil {
38+
t.Errorf("expected no error, but got: %v", err)
39+
}
40+
41+
// Validate the cli output
42+
got := strings.TrimSpace(string(output)[:36])
43+
if got != expectedOutput {
44+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
45+
}
46+
47+
}

tests/techcrunch_test.go

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package test
2+
3+
import (
4+
"os/exec"
5+
"strings"
6+
"testing"
7+
)
8+
9+
// TestTcCmd tests the tc command
10+
func TestTcCmd(t *testing.T) {
11+
12+
expectedOutput := "Searching for latest blogs on techcrunch...."
13+
cmd := exec.Command("scrapy", "tc")
14+
15+
// Capture the output
16+
output, err := cmd.CombinedOutput()
17+
if err != nil {
18+
t.Errorf("expected no error, but got: %v", err)
19+
}
20+
21+
// Validate the cli output
22+
got := strings.TrimSpace(string(output)[:44])
23+
if got != strings.Trim(expectedOutput, " ") {
24+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
25+
}
26+
27+
}
28+
29+
// TestTcCmdWithCategoryFlag tests the tc command with the category flag
30+
31+
func TestTcCmdWithCategoryFlag(t *testing.T) {
32+
33+
expectedOutput := "Searching for latest blogs on techcrunch...."
34+
cmd := exec.Command("scrapy", "tc", "--category", "startups")
35+
36+
// Capture the output
37+
output, err := cmd.CombinedOutput()
38+
if err != nil {
39+
t.Errorf("expected no error, but got: %v", err)
40+
}
41+
42+
// Validate the 1st line of the cli output
43+
got := strings.TrimSpace(string(output)[:44])
44+
if got != strings.Trim(expectedOutput, " ") {
45+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
46+
}
47+
48+
}
49+
50+
// // TestTcCmdWithInvalidCategoryFlag tests the tc command with the category flag
51+
func TestTcCmdWithInvalidCategoryFlag(t *testing.T) {
52+
53+
expectedOutput := "Searching for latest blogs on techcrunch...."
54+
stringsContains := "Invalid category. Showing homepage instead."
55+
cmd := exec.Command("scrapy", "tc", "--category", "football")
56+
57+
// Capture the output
58+
output, err := cmd.CombinedOutput()
59+
if err != nil {
60+
t.Errorf("expected no error, but got: %v", err)
61+
}
62+
63+
// Validate the 1st line of the cli output
64+
got := strings.TrimSpace(string(output)[:44])
65+
if got != strings.Trim(expectedOutput, " ") {
66+
t.Errorf("expected %v, but got: %v", expectedOutput, got)
67+
}
68+
69+
// Validate the 2nd line of the cli output
70+
if strings.Contains(string(output), stringsContains) != true {
71+
t.Errorf("expected %v, but got: %v", stringsContains, string(output))
72+
}
73+
74+
}

0 commit comments

Comments
 (0)