Skip to content

Commit

Permalink
✅ fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bhupesh-V committed Jun 21, 2022
1 parent 9b37f3b commit b9a736c
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 27 deletions.
42 changes: 25 additions & 17 deletions areyouok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
"fmt"
"reflect"
"testing"

"github.com/Bhupesh-V/areyouok/git"
"github.com/Bhupesh-V/areyouok/links"
"github.com/Bhupesh-V/areyouok/utils"
)

func Test_in(t *testing.T) {
func Test_In(t *testing.T) {
t.Run("in", func(t *testing.T) {
ans := in("test", []string{"test", "sample"})
ans := utils.In("test", []string{"test", "sample"})
if ans != true {
t.Errorf("In() want %s, want %s", "false", "true")
}
})
t.Run("not in", func(t *testing.T) {
//...
ans := in("nice", []string{"test", "sample"})
ans := utils.In("nice", []string{"test", "sample"})
if ans != false {
t.Errorf("In() want %s, want %s", "true", "false")
}
Expand All @@ -32,7 +35,7 @@ func TestRegExGood(t *testing.T) {
"http://website.in/843783787",
}
for _, url := range goodUrls {
ans := re.MatchString(url)
ans := links.GetURLRegex().MatchString(url)
if ans != true {
t.Errorf("RegEx %s want %s got %s", url, "true", "false")
}
Expand All @@ -46,7 +49,7 @@ func TestRegExBad(t *testing.T) {
"example.com/file[/].html",
}
for _, url := range badUrls {
ans := re.MatchString(url)
ans := links.GetURLRegex().MatchString(url)
if ans != false {
t.Errorf("RegEx %s want %s got %s", url, "false", "true")
}
Expand Down Expand Up @@ -85,20 +88,23 @@ func AreEqualJSON(s1, s2 string) (bool, error) {

func Test_getGitDetails(t *testing.T) {
userPath := "."
getGitDetails(&userPath)

correctRemote := "https://github.com/Bhupesh-V/areyouok"
correctBranch := "master"
if branchName != correctBranch {
t.Errorf("GitDetails want %s got %s", correctBranch, branchName)

ansBranch, _ := git.GetGitBranch(&userPath)
ansRemote, _ := git.GetGitRemoteURL(&userPath)

if correctBranch != ansBranch {
t.Errorf("GitDetails want %s got %s", correctBranch, ansBranch)
}
if repoURL != correctRemote {
t.Errorf("GitDetails want %s got %s", correctRemote, repoURL)
if correctRemote != ansRemote {
t.Errorf("GitDetails want %s got %s", correctRemote, ansRemote)
}
}

func Test_getValidFiles(t *testing.T) {

userPath := "."
t.Run("With no ignore", func(t *testing.T) {
valid := []string{
".github/ISSUE_TEMPLATE/----bug-report.md",
Expand All @@ -111,9 +117,9 @@ func Test_getValidFiles(t *testing.T) {
"CONTRIBUTING.md",
"README.md",
}
ans := getFiles(&userPath, "md", []string{""})
ans := links.GetFiles("md", []string{""})
if !Equal(valid, ans) {
t.Errorf("getValidFiles() want %s got %s", valid, ans)
t.Errorf("GetValidFiles() want %s got %s", valid, ans)
}
})
t.Run("With multiple ignore", func(t *testing.T) {
Expand All @@ -122,15 +128,17 @@ func Test_getValidFiles(t *testing.T) {
"CONTRIBUTING.md",
"README.md",
}
ans := getFiles(&userPath, "md", []string{".github", "CHANGELOG.md"})
ans := links.GetFiles("md", []string{".github", "CHANGELOG.md"})
if !Equal(valid, ans) {
t.Errorf("getValidFiles() want %s got %s", valid, ans)
t.Errorf("GetValidFiles() want %s got %s", valid, ans)
}
})
}

func Test_getLinks(t *testing.T) {
a1, a2 := getLinks([]string{"CODE_OF_CONDUCT.md"}, ".")
links := links.GetLinks([]string{"CODE_OF_CONDUCT.md"})
a1 := links.AllHyperlinks
a2 := links.FileToListOfLinks

t.Run("check file path based JSON", func(t *testing.T) {
valid := []map[string]string{
Expand Down
6 changes: 5 additions & 1 deletion links/links.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ type Links struct {
AllHyperlinks []map[string]string
}

func GetURLRegex() *regexp.Regexp {
return regexp.MustCompile(`(http|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
}

// Get all valid links
func GetLinks(files []string) *Links {
var totalLinks int
var linkRegex = regexp.MustCompile(`(http|https):\/\/([\w\-_]+(?:(?:\.[\w\-_]+)+))([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?`)
var linkRegex = GetURLRegex()
// Map of file to list of links
hyperlinks := make(map[string][]string)
// List of JSON {file, url}
Expand Down
6 changes: 3 additions & 3 deletions report/formats/report_html.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<html>
<head>
<title>areyouok: are your links ok?</title>
<title>AreYouOk: Are your links ok?</title>
<style>
summary {
font-size: x-large;
Expand Down Expand Up @@ -51,10 +51,10 @@ <h1>AreYouOk Links Health Report ⛑️ </h1>
<p class="description">
<b>{{.TotalLinks}} URL(s) were analyzed across {{.TotalFiles}} file(s) in {{.TotalTime}}</b>
</p>
<!-- {{ $length := len $.CompleteHealthData }} {{ if eq $length 0 }}
{{ $length := len $.NotOkLinks }} {{ if eq $length 0 }}
Sorry. No matching results found
{{ end }}
{{ $length }} -->
{{ $length }}
<details open><summary>Not OK URLs</summary>
<table>
<tr>
Expand Down
11 changes: 6 additions & 5 deletions report/formats/report_txt.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{{.TotalLinks}} URLs were analyzed across {{.TotalFiles}} files in {{ println .TotalTime}}{{"\n"}}
{{.TotalLinks}} URLs were analyzed across {{.TotalFiles}} files in {{.TotalTime}}{{"\n"}}
Following URLs were found not OK:{{"\n\n"}}
{{- range $url, $v := $.ReLinks}}
{{- if ne (index $v "message") "OK" }}
{{- print $url "\n" }}
{{- end}}

{{- range $url, $v := $.CompleteHealthData}}
{{- if ne (index $v "message") "OK"}}
{{print $url -}}
{{- end}}
{{- end}}
4 changes: 3 additions & 1 deletion report/report.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ func (r *Report) GenerateReport() {
os.Exit(1)
}
} else if *r.ReportType == "txt" {
fmt.Println(*r.ReportData.TotalTime)
textReport := textTemplate.Must(textTemplate.ParseFS(reportTemplates, fmt.Sprintf("formats/report_%s.txt", *r.ReportType)))
f, _ := os.Create("report.txt")
f, err := os.Create("report.txt")
utils.CheckErr(err)
textReport.Execute(f, r.ReportData)
}
fmt.Printf("\nReport Generated: %s.%s", filepath.Join(currentDir, "report"), *r.ReportType)
Expand Down

0 comments on commit b9a736c

Please sign in to comment.