Skip to content

Commit

Permalink
Start implementation, refs #8
Browse files Browse the repository at this point in the history
  • Loading branch information
rednafi committed Jan 22, 2024
1 parent 3a172e9 commit 58b8433
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 26 deletions.
5 changes: 4 additions & 1 deletion src/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,10 @@ func checkLinks(
}

// Orchestrate the whole process
func orchestrate(w *tabwriter.Writer, filepath string, timeout time.Duration, skipRelative bool, errOk bool) {
func orchestrate(
w *tabwriter.Writer, filepath string, timeout time.Duration,
skipRelative bool, errOk bool,
) {
defer w.Flush()

printFilepath(w, filepath)
Expand Down
133 changes: 108 additions & 25 deletions src/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package src

import (
"bytes"
"fmt"
"log"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -94,6 +95,81 @@ func TestReadMarkdown_NonMarkdownFile(t *testing.T) {
}
}

func TestNewLinkRecord(t *testing.T) {
tests := []struct {
name string
link string
wantType linkType
wantOk bool
wantErr string
}{
{
name: "Valid HTTP URL",
link: "http://example.com",
wantType: urlType,
wantOk: false,
wantErr: "",
},
{
name: "Valid HTTPS URL",
link: "https://example.com",
wantType: urlType,
wantOk: false,
wantErr: "",
},
{
name: "Invalid URL",
link: "htt://example.com",
wantType: urlType,
wantOk: false,
wantErr: "",
},
// {
// name: "Valid Relative Filepath",
// link: "./test.md",
// wantType: filepathType,
// wantOk: false,
// wantErr: false,
// },
// {
// name: "Valid Absolute Filepath",
// link: "/tmp/test.md",
// wantType: filepathType,
// wantOk: false,
// wantErr: false,
// },
// {
// name: "Invalid Filepath",
// link: "invalidpath/test",
// wantType: "",
// wantOk: false,
// wantErr: true,
// },
// {
// name: "Windows Style Absolute Path",
// link: "C:\\test.md",
// wantType: filepathType,
// wantOk: false,
// wantErr: false,
// },
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := newLinkRecord(tt.link)
if got.ErrMsg != tt.wantErr {
t.Errorf("newLinkRecord() error = %v, wantErr %v", got.ErrMsg, tt.wantErr)
}
if got.Type != tt.wantType {
t.Errorf("newLinkRecord() Type = %v, wantType %v", got.Type, tt.wantType)
}
if got.Ok != tt.wantOk {
t.Errorf("newLinkRecord() Ok = %v, wantOk %v", got.Ok, tt.wantOk)
}
})
}
}

// TestCheckLink_Success tests the checkUrl function with a successful HTTP request
func TestCheckLink_Success(t *testing.T) {
t.Parallel()
Expand Down Expand Up @@ -188,36 +264,40 @@ func TestCheckLinks(t *testing.T) {
buf := new(bytes.Buffer)
w := tabwriter.NewWriter(buf, 0, 0, 2, ' ', 0)

// Create a test server that always returns a specific status code
// Create a test server returning a status code
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()

// Create a list of test URLs
// List of test URLs
urls := []string{ts.URL + "/ok", ts.URL + "/invalid-url"}

// Set the timeout and error flag for testing
// Set timeout and error flag for testing
timeout := time.Second
errOk := false

// Call the checkLinks function
// Call checkLinks function
_ = checkLinks(w, urls, timeout, errOk)

// Flush the tabwriter.Writer to get the output
// Flush tabwriter.Writer for output
w.Flush()
output := buf.String()

// Verify the output
expectedOutput1 := "- Type : url\n location : http://127.0.0.1:56111/invalid-url\n Status Code : 200\n Ok : true\n Error : -\n\n- Type : url\n location : http://127.0.0.1:56111/ok\n Status Code : 200\n Ok : true\n Error : -\n\n"

expectedOutput2 := "- Type : url\n location : http://127.0.0.1:56133/ok\n Status Code : 200\n Ok : true\n Error : -\n\n- Type : url\n location : http://127.0.0.1:56133/invalid-url\n Status Code : 200\n Ok : true\n Error : -\n\n"

if output != expectedOutput1 && output != expectedOutput2 {
t.Errorf(
"checkLinks() = %q, want %q or %q",
output, expectedOutput1, expectedOutput2,
)
expOutput1 := fmt.Sprintf("- Type : url\n location : %s/invalid-url\n"+
" Status Code : 200\n Ok : true\n Error : -\n\n"+
"- Type : url\n location : %s/ok\n Status Code : 200\n"+
" Ok : true\n Error : -\n\n", ts.URL, ts.URL)

expOutput2 := fmt.Sprintf("- Type : url\n location : %s/ok\n"+
" Status Code : 200\n Ok : true\n Error : -\n\n"+
"- Type : url\n location : %s/invalid-url\n Status Code : 200\n"+
" Ok : true\n Error : -\n\n", ts.URL, ts.URL)

if output != expOutput1 && output != expOutput2 {
t.Errorf("checkLinks() = %q, want %q or %q",
output, expOutput1, expOutput2)
}
}

Expand Down Expand Up @@ -307,9 +387,11 @@ func TestCheckLinks_RaisesError(t *testing.T) {
}
}

// Test for printUrlState function
// Test for printLinkRecord function
func TestPrintLinkRecord(t *testing.T) {
t.Parallel()

// Create a slice of linkRecords
linkRecords := []linkRecord{
{"url", "http://example.com", 200, true, ""},
{"url", "http://testsite.com", 404, false, "Not Found"},
Expand All @@ -318,21 +400,22 @@ func TestPrintLinkRecord(t *testing.T) {
var buf bytes.Buffer
w := tabwriter.NewWriter(&buf, 0, 0, 1, ' ', 0)

for _, linkRecord := range linkRecords {
printLinkRecord(w, linkRecord)
// Process each linkRecord
for _, lr := range linkRecords {
printLinkRecord(w, lr)
}
w.Flush()

expectedOutput := "- URL : http://example.com\n" +
" Status Code: 200\n" +
" Error : OK\n\n" +
"- URL : http://testsite.com\n" +
" Status Code: 404\n" +
" Error : Not Found\n\n"
// Define expected output
expOutput := "- Type : url\n location : http://example.com\n" +
" Status Code : 200\n Ok : true\n Error : -\n\n" +
"- Type : url\n location : http://testsite.com\n" +
" Status Code : 404\n Ok : false\n Error : Not Found\n\n"
actualOutput := buf.String()

if actualOutput != expectedOutput {
t.Errorf("printUrlState() = %q, want %q", actualOutput, expectedOutput)
// Assert output correctness
if actualOutput != expOutput {
t.Errorf("printLinkRecord() = %q, want %q", actualOutput, expOutput)
}
}

Expand Down

0 comments on commit 58b8433

Please sign in to comment.