Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rednafi committed Jan 20, 2024
1 parent d413d7f commit e26ad14
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 33 deletions.
10 changes: 5 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
linters:
enable:
- gofmt
- gofumpt
- nolintlint
- lll
- gofmt
- gofumpt
- nolintlint
- lll

issues:
max-issues-per-linter: 0
Expand All @@ -12,4 +12,4 @@ issues:
linters-settings:
lll:
line-length: 92 # Set your desired max line length here
tab-width: 4 # Optional: set the width of tab characters
tab-width: 4 # Optional: set the width of tab characters
10 changes: 5 additions & 5 deletions .goreleaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ archives:
{{- if .Arm }}v{{ .Arm }}_{{ .Version }}{{ end }}
# use zip for windows archives
format_overrides:
- goos: windows
format: zip
- goos: windows
format: zip

checksum:
name_template: 'checksums.txt'
name_template: "checksums.txt"

snapshot:
name_template: "{{ incpatch .Version }}-next"
Expand All @@ -38,8 +38,8 @@ changelog:
sort: asc
filters:
exclude:
- '^docs:'
- '^test:'
- "^docs:"
- "^test:"

brews:
- name: link-patrol
Expand Down
15 changes: 15 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
public/
static/
layouts/
themes/

# Ignore
**/*.html
**/Makefile
.editorconfig
.prettierignore
.git*
.hugo*
Brew*
pyproject*
requirements*
24 changes: 24 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"proseWrap": "preserve",
"semi": false,
"singleQuote": false,
"useTabs": false,
"tabWidth": 2,
"trailingComma": "all",
"printWidth": 92,
"overrides": [
{
"files": "**/*.md",
"options": {
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"trailingComma": "all",
"arrowParens": "avoid",
"printWidth": 92,
"proseWrap": "always",
"embeddedLanguageFormatting": "off"
}
}
]
}
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ lint:
@echo "Running lint"
@golangci-lint run --fix
@go mod tidy
@prettier --write .


.PHONY: lint-check
Expand Down
22 changes: 11 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ Detect dead links in markdown files

## Installation

* On MacOS, brew install:
- On MacOS, brew install:

```sh
brew tap rednafi/link-patrol https://github.com/rednafi/link-patrol \
&& brew install link-patrol
```
```sh
brew tap rednafi/link-patrol https://github.com/rednafi/link-patrol \
&& brew install link-patrol
```

* Or elsewhere, go install:
- Or elsewhere, go install:

```sh
go install github.com/rednafi/link-patrol/cmd/link-patrol
```
```sh
go install github.com/rednafi/link-patrol/cmd/link-patrol
```

## Quickstart

Expand Down Expand Up @@ -77,8 +77,8 @@ Run the following command to list the URL status with a 2 second timeout for eac
link-patrol -f examples/sample_1.md -t 2s
```
By default, it'll exit with a non-zero code if any of the URLs is invalid or unreachable. Here's
how the output looks:
By default, it'll exit with a non-zero code if any of the URLs is invalid or unreachable.
Here's how the output looks:
```txt
Link patrol
Expand Down
1 change: 1 addition & 0 deletions examples/sample_1.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ This is a [reference style] URL.
This is a footnote[^1] URL.

[reference style]: https://reference.com

[^1]: https://gen.xyz/
14 changes: 13 additions & 1 deletion examples/sample_2.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
# Comprehensive Markdown File with Various URL Definitions

## Inline URLs

This is an [inline URL](https://example.com).

## Reference Style URLs

This is a [reference style URL][ref-style].

## Reference Style with Title

This is a [reference with title][ref-title].

## Footnote Style URLs

This is a footnote[^1] URL.

## Image URLs

This is an image URL: ![Alt text](https://example.com/image.jpg "Image Title")

## Image with Reference Style

This is a referenced image: ![Alt text][image-ref]

## Long URL
This is a [long URL](https://www.terminusapp.com/blog/short-urls-vs-long-urls/#:~:text=On%20the%20other%20hand%2C%20long,folders%2C%20subfolders%2C%20and%20parameters.)

This is a
[long URL](https://www.terminusapp.com/blog/short-urls-vs-long-urls/#:~:text=On%20the%20other%20hand%2C%20long,folders%2C%20subfolders%2C%20and%20parameters.)

## Automatic URL Linking

<https://example.com>

## Email Address Linking

<mailto:[email protected]>

## Reference Definitions

[ref-style]: https://referencestyle.com
[ref-title]: https://referencestyle.com "Reference Style with Title"
[image-ref]: https://example.com/image.jpg

[^1]: https://footnote.com
1 change: 1 addition & 0 deletions examples/sample_3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<!-- An empty markdown file -->
Empty file added examples/sample_4.html
Empty file.
29 changes: 20 additions & 9 deletions src/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func readMarkdown(filepath string) ([]byte, error) {
}

// Extract URLs from markdown content
func findLinks(markdown []byte) []string {
func findLinks(markdown []byte) ([]string, error) {
var links []string

// Parse the markdown content
Expand Down Expand Up @@ -62,10 +62,10 @@ func findLinks(markdown []byte) []string {
return ast.WalkContinue, nil
})
if err != nil {
log.Fatal(err)
return links, err
}

return links
return links, nil
}

// Check the state of a URL and save the result in a struct
Expand Down Expand Up @@ -137,7 +137,9 @@ func printLinkRecord(w *tabwriter.Writer, linkRecord linkRecord) {
}

// Check the state of a list of URLs
func checkLinks(w *tabwriter.Writer, urls []string, timeout time.Duration, errOk bool) {
func checkLinks(
w *tabwriter.Writer, urls []string, timeout time.Duration, errOk bool,
) error {
var wg sync.WaitGroup
var mutex sync.Mutex
var hasError bool
Expand All @@ -155,29 +157,39 @@ func checkLinks(w *tabwriter.Writer, urls []string, timeout time.Duration, errOk
if urlState.StatusCode >= 400 {
hasError = true
}

mutex.Unlock()
wg.Done()
}(url)
}
wg.Wait()

if hasError && !errOk {
log.Fatal("Some URLs are invalid or unreachable")
return fmt.Errorf("one or more URLs have an error status code")
}
return nil
}

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

printFilepath(w, filepath)

// Read markdown file from filepath
markdown, err := readMarkdown(filepath)
if err != nil {
log.Fatal(err)
}

urls := findLinks(markdown)
checkLinks(w, urls, timeout, errOk)
urls, err := findLinks(markdown)
if err != nil {
log.Fatal(err)
}

err = checkLinks(w, urls, timeout, errOk)
if err != nil {
log.Fatal(err)
}
}

// CLI
Expand Down Expand Up @@ -234,7 +246,6 @@ func Cli(w *tabwriter.Writer, version string, exitFunc func(int)) {
}

// Proceed with orchestration as filepath is provided
printFilepath(w, filepath)
orchestrate(w, filepath, timeout, errOk) // Your orchestrate function
return nil
}
Expand Down
Loading

0 comments on commit e26ad14

Please sign in to comment.