From ac610f88d340fae0e0bc9e18fd0c66ebdb6bfdd1 Mon Sep 17 00:00:00 2001 From: azimut Date: Tue, 11 Apr 2023 23:04:17 -0300 Subject: [PATCH] move simple fetch tests to "go test" --- .github/workflows/main.yml | 1 - Makefile | 20 ++++---------------- cmd/twitterview/main.go | 2 +- internal/fetch/fetch_test.go | 15 +++++++++++++++ internal/hackernews/fetch_test.go | 15 +++++++++++++++ internal/reddit/fetch_test.go | 15 +++++++++++++++ internal/reddit/format.go | 2 +- internal/twitter/{tweet.go => fetch.go} | 0 internal/twitter/fetch_test.go | 15 +++++++++++++++ 9 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 internal/fetch/fetch_test.go create mode 100644 internal/hackernews/fetch_test.go create mode 100644 internal/reddit/fetch_test.go rename internal/twitter/{tweet.go => fetch.go} (100%) create mode 100644 internal/twitter/fetch_test.go diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 3872ea9..c8f3ef9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -25,4 +25,3 @@ jobs: go-version: 1.17 - run: | make test - make testrun > /dev/null diff --git a/Makefile b/Makefile index bf3e5a1..2ea4a64 100644 --- a/Makefile +++ b/Makefile @@ -1,31 +1,19 @@ -HN_URL := 'https://news.ycombinator.com/item?id=3078128' -TW_URL := 'https://twitter.com/TwitterDev/status/1443269993676763138' GO_FILES = $(shell find . -type f -name '*.go') .PHONY: all install clean test testrun all: twitterview hackerview redditview -hackerview: $(GO_FILES) - go build -race -v -ldflags="-s -w" ./cmd/$@ - ls -lh $@ - +hackerview: +redditview: twitterview: $(GO_FILES) go build -v -ldflags="-s -w" ./cmd/$@ ls -lh $@ -redditview: $(GO_FILES) - go build -v -ldflags="-s -w" ./cmd/$@ - ls -lh $@ - install: hackerview twitterview redditview mv hackerview $(HOME)/go/bin/ mv twitterview $(HOME)/go/bin/ - mv redditview $(HOME)/go/bin/ + mv redditview $(HOME)/go/bin/ clean: ; go clean -x ./... -test: ; go test -vet=all -v ./... - -testrun: - go run cmd/hackerview/main.go -l 10 -t 20s $(HN_URL) - go run cmd/twitterview/main.go -t 20s $(TW_URL) +test: ; go test -vet=all -v -race ./... diff --git a/cmd/twitterview/main.go b/cmd/twitterview/main.go index e358179..1a54f41 100644 --- a/cmd/twitterview/main.go +++ b/cmd/twitterview/main.go @@ -25,7 +25,7 @@ var url string func init() { flag.DurationVar(&opt.timeout, "t", time.Second*5, "timeout in seconds") - flag.StringVar(&opt.userAgent, "A", "Wget", "default User-Agent sent") + flag.StringVar(&opt.userAgent, "A", "Twitter_View/0.1", "default User-Agent sent") flag.BoolVar(&opt.useColors, "C", true, "use colors") flag.BoolVar(&opt.usePretty, "P", true, "use pretty formatting") flag.IntVar(&opt.width, "w", 0, "fixed with, defaults to console width") diff --git a/internal/fetch/fetch_test.go b/internal/fetch/fetch_test.go new file mode 100644 index 0000000..64bb0c5 --- /dev/null +++ b/internal/fetch/fetch_test.go @@ -0,0 +1,15 @@ +package fetch + +import ( + "testing" + "time" +) + +const URL = `https://www.google.com/robots.txt` + +func TestFech(t *testing.T) { + _, err := Fetch(URL, "Wget", time.Second*10) + if err != nil { + t.Fail() + } +} diff --git a/internal/hackernews/fetch_test.go b/internal/hackernews/fetch_test.go new file mode 100644 index 0000000..6ff8291 --- /dev/null +++ b/internal/hackernews/fetch_test.go @@ -0,0 +1,15 @@ +package hackernews + +import ( + "testing" + "time" +) + +const URL = `https://news.ycombinator.com/item?id=3078128` + +func TestHNFetch(t *testing.T) { + _, _, err := Fetch(URL, time.Second*10, 10, 2) + if err != nil { + t.Fail() + } +} diff --git a/internal/reddit/fetch_test.go b/internal/reddit/fetch_test.go new file mode 100644 index 0000000..7baaa91 --- /dev/null +++ b/internal/reddit/fetch_test.go @@ -0,0 +1,15 @@ +package reddit + +import ( + "testing" + "time" +) + +const URL = `https://old.reddit.com/r/gamedev/comments/horm1a/ive_started_making_a_teaching_aid_for_people/` + +func TestRTFetch(t *testing.T) { + _, err := Fetch(URL, "Reddit_Cli/0.1", time.Second*10) + if err != nil { + t.Fail() + } +} diff --git a/internal/reddit/format.go b/internal/reddit/format.go index eda8cdd..4e7b2fe 100644 --- a/internal/reddit/format.go +++ b/internal/reddit/format.go @@ -31,7 +31,7 @@ func (op Op) String() (ret string) { ret += "\n" ret += fixupContent(op.selftext, MaxWidth, Padding) ret += "\n" - ret += fmt.Sprintf("%s(%d) - %s - %d Comment(s)\n\n\n\n", + ret += fmt.Sprintf("%s(%d) - %s - %d Comment(s)\n\n\n", color.New(AuthorColor).Sprint(op.author), op.upvotes, relativeFromUnix(op.createdUTC), diff --git a/internal/twitter/tweet.go b/internal/twitter/fetch.go similarity index 100% rename from internal/twitter/tweet.go rename to internal/twitter/fetch.go diff --git a/internal/twitter/fetch_test.go b/internal/twitter/fetch_test.go new file mode 100644 index 0000000..0f949c8 --- /dev/null +++ b/internal/twitter/fetch_test.go @@ -0,0 +1,15 @@ +package twitter + +import ( + "testing" + "time" +) + +const URL = `https://twitter.com/TwitterDev/status/1443269993676763138` + +func TestTWFetch(t *testing.T) { + _, err := Fetch(URL, "Twitter_View/0.1", time.Second*10) + if err != nil { + t.Fail() + } +}