Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--format option #298

Merged
merged 12 commits into from
Apr 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npx markdown-link-check README.md
- uses: lycheeverse/lychee-action@v1
with:
fail: true
1 change: 1 addition & 0 deletions .snapshots/TestHelp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Application Options:
sitemap.xml
--header=<header>... Custom headers
-f, --ignore-fragments Ignore URL fragments
--format=[text|json|junit] Output format (default: text)
--json Output results in JSON
--experimental-verbose-json Include successful results in JSON
--junit Output results as JUnit XML file
Expand Down
19 changes: 18 additions & 1 deletion arguments.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ type arguments struct {
FollowRobotsTxt bool `long:"follow-robots-txt" description:"Follow robots.txt when scraping pages"`
FollowSitemapXML bool `long:"follow-sitemap-xml" description:"Scrape only pages listed in sitemap.xml"`
RawHeaders []string `long:"header" value-name:"<header>..." description:"Custom headers"`
IgnoreFragments bool `short:"f" long:"ignore-fragments" description:"Ignore URL fragments"`
// TODO Remove a short option.
IgnoreFragments bool `short:"f" long:"ignore-fragments" description:"Ignore URL fragments"`
Format string `long:"format" description:"Output format" default:"text" choice:"text" choice:"json" choice:"junit"`
// TODO Merge text, JSON, and JUnit XML format options into --format.
JSONOutput bool `long:"json" description:"Output results in JSON"`
// TODO Integrate this option into --verbose.
Expand Down Expand Up @@ -53,6 +55,8 @@ func getArguments(ss []string) (*arguments, error) {
return nil, errors.New("invalid number of arguments")
}

reconcileDeprecatedArguments(&args)

args.URL = ss[0]

args.ExcludedPatterns, err = compileRegexps(args.RawExcludedPatterns)
Expand All @@ -70,6 +74,10 @@ func getArguments(ss []string) (*arguments, error) {
return nil, err
}

if args.Format == "junit" && args.Verbose {
return nil, errors.New("verbose option not supported for JUnit output")
}

return &args, nil
}

Expand Down Expand Up @@ -116,3 +124,12 @@ func parseHeaders(headers []string) (map[string]string, error) {

return m, nil
}

func reconcileDeprecatedArguments(args *arguments) {
if args.JSONOutput {
args.Format = "json"
args.Verbose = args.Verbose || args.VerboseJSON
} else if args.JUnitOutput {
args.Format = "junit"
}
}
15 changes: 5 additions & 10 deletions command.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ func (c *command) runWithError(ss []string) (bool, error) {
return true, nil
} else if args.Version {
c.print(version)
return err == nil, err
} else if args.JSONOutput && args.Verbose {
return false, errors.New("verbose option not supported for JSON output")
} else if args.JUnitOutput && args.Verbose {
return false, errors.New("verbose option not supported for JUnit output")
} else if args.JSONOutput && args.JUnitOutput {
return false, errors.New("JSON and JUnit output are mutually exclusive")
return true, nil
}

client := newRedirectHttpClient(
Expand Down Expand Up @@ -115,9 +109,10 @@ func (c *command) runWithError(ss []string) (bool, error) {

go checker.Check(p)

if args.JSONOutput {
return c.printResultsInJSON(checker.Results(), args.VerboseJSON)
} else if args.JUnitOutput {
switch args.Format {
case "json":
return c.printResultsInJSON(checker.Results(), args.Verbose)
case "junit":
return c.printResultsInJUnitXML(checker.Results())
}

Expand Down
22 changes: 0 additions & 22 deletions command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,17 +270,6 @@ func TestCommandIncludeSuccessfulPageInJSONOutputWhenRequested(t *testing.T) {
assert.Equal(t, strings.TrimSpace(b.String()), "[{\"url\":\"\",\"links\":[]}]")
}

func TestCommandFailWithVerboseAndJSONOptions(t *testing.T) {
b := &bytes.Buffer{}

ok := newTestCommandWithStderr(b, nil).Run(
[]string{"--json", "--verbose", "http://foo.com"},
)

assert.False(t, ok)
cupaloy.SnapshotT(t, b.String())
}

func TestCommandFailToRunWithJUnitOutput(t *testing.T) {
b := &bytes.Buffer{}

Expand Down Expand Up @@ -312,14 +301,3 @@ func TestCommandFailWithVerboseAndJUnitOptions(t *testing.T) {
assert.False(t, ok)
cupaloy.SnapshotT(t, b.String())
}

func TestCommandFailWithJUnitAndJSONOptions(t *testing.T) {
b := &bytes.Buffer{}

ok := newTestCommandWithStderr(b, nil).Run(
[]string{"--json", "--junit", "http://foo.com"},
)

assert.False(t, ok)
cupaloy.SnapshotT(t, b.String())
}