Skip to content

Commit

Permalink
Merge pull request #4 from bwplotka/fmt-impr
Browse files Browse the repository at this point in the history
Improved formatted and tests.
  • Loading branch information
bwplotka authored Nov 29, 2020
2 parents 9f4a39b + 9e18371 commit d895183
Show file tree
Hide file tree
Showing 24 changed files with 181 additions and 66 deletions.
3 changes: 3 additions & 0 deletions .errcheck_excludes.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(github.com/go-kit/kit/log.Logger).Log
fmt.Fprintln
fmt.Fprint
76 changes: 76 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This file contains all available configuration options
# with their default values.

# options for analysis running
run:
# timeout for analysis, e.g. 30s, 5m, default is 1m
deadline: 5m

# exit code when at least one issue was found, default is 1
issues-exit-code: 1

# which dirs to skip: they won't be analyzed;
# can use regexp here: generated.*, regexp is applied on full path;
# default value is empty list, but next dirs are always skipped independently
# from this option's value:
# vendor$, third_party$, testdata$, examples$, Godeps$, builtin$
skip-dirs: vendor

# output configuration options
output:
# colored-line-number|line-number|json|tab|checkstyle, default is "colored-line-number"
format: colored-line-number

# print lines of code with issue, default is true
print-issued-lines: true

# print linter name in the end of issue text, default is true
print-linter-name: true

linters:
enable:
# Sorted alphabetically.
- deadcode
- errcheck
- goconst
- godot
- gofmt
- goimports
- gosimple
- govet
- ineffassign
- misspell
- staticcheck
- structcheck
- typecheck
- unparam
- unused
- varcheck

linters-settings:
errcheck:
exclude: ./.errcheck_excludes.txt
misspell:
locale: US
goconst:
min-occurrences: 5

issues:
exclude-rules:
# These are not being checked since these methods exist
# so that no one else could implement them.
- linters:
- unused
text: "SourceStoreAPI.implementsStoreAPI"
- linters:
- unused
text: "SourceStoreAPI.producesBlocks"
- linters:
- unused
text: "Source.producesBlocks"
- linters:
- unused
text: "newMockAlertmanager"
- linters:
- unused
text: "ruleAndAssert"
5 changes: 2 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

NOTE: As semantic versioning states all 0.y.z releases can contain breaking changes in API (flags, grpc API, any backward compatibility)

We use *breaking* word for marking changes that are not backward compatible (relates only to v0.y.z releases.)

## Unreleased
## [v0.1.0]()

Initial release.
13 changes: 7 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ FILES_TO_FMT ?= $(shell find . -path ./vendor -prune -o -name '*.go' -print
GO111MODULE ?= on
export GO111MODULE

GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin
GOBIN ?= $(firstword $(subst :, ,${GOPATH}))/bin

# Tools.
GIT ?= $(shell which git)
GIT ?= $(shell which git)

# Support gsed on OSX (installed via brew), falling back to sed. On Linux
# systems gsed won't be installed, so will use sed as expected.
Expand Down Expand Up @@ -41,7 +41,7 @@ all: format build
.PHONY: build
build: ## Build mdox.
@echo ">> building mdox"
@go install github.com/bwplotka/mdox
@GOBIN=$(GOBIN) go install github.com/bwplotka/mdox

.PHONY: check-comments
check-comments: ## Checks Go code comments if they have trailing period (excludes protobuffers and vendor files). Comments with more than 3 spaces at beginning are omitted from the check, example: '// - foo'.
Expand All @@ -55,7 +55,8 @@ deps: ## Ensures fresh go.mod and go.sum.

.PHONY: docs
docs: build ## Generates config snippets and doc formatting.
@$(GOBIN)/mdox fmt *.md
@echo ">> generating docs $(PATH)"
@PATH=$(GOBIN) mdox fmt *.md

.PHONY: format
format: ## Formats Go code including imports and cleans up white noise.
Expand Down Expand Up @@ -86,7 +87,7 @@ endif
# --mem-profile-path string Path to memory profile output file
# to debug big allocations during linting.
lint: ## Runs various static analysis against our code.
lint: $(FAILLINT) $(GOLANGCI_LINT) $(MISSPELL) format docs check-git deps
lint: $(FAILLINT) $(GOLANGCI_LINT) $(MISSPELL) build format docs check-git deps
$(call require_clean_work_tree,"detected not clean master before running lint")
@echo ">> verifying modules being imported"
@$(FAILLINT) -paths "errors=github.com/pkg/errors" ./...
Expand All @@ -101,4 +102,4 @@ lint: $(FAILLINT) $(GOLANGCI_LINT) $(MISSPELL) format docs check-git deps
@find . -type f \( -name "*.md" -o -name "*.go" \) | SED_BIN="$(SED)" xargs scripts/cleanup-white-noise.sh
@echo ">> ensuring Copyright headers"
@go run ./scripts/copyright/...
$(call require_clean_work_tree,"detected white noise or/and files without copyright; run 'make lint' file and commit changes.")
$(call require_clean_work_tree,"detected white noise or/and files without copyright; run make lint file and commit changes.")
15 changes: 6 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
# mdox

[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/bwplotka/mdox)
[![Latest Release](https://img.shields.io/github/release/bwplotka/mdox.svg?style=flat-square)](https://github.com/bwplotka/mdox/releases/latest)
[![CI](https://github.com/bwplotka/mdox/workflows/go/badge.svg)](https://github.com/bwplotka/mdox/actions?query=workflow%3Ago)
[![Go Report Card](https://goreportcard.com/badge/github.com/bwplotka/mdox)](https://goreportcard.com/report/github.com/bwplotka/mdox)
[![go.dev reference](https://img.shields.io/badge/go.dev-reference-007d9c?logo=go&logoColor=white&style=flat-square)](https://pkg.go.dev/github.com/bwplotka/mdox) [![Latest Release](https://img.shields.io/github/release/bwplotka/mdox.svg?style=flat-square)](https://github.com/bwplotka/mdox/releases/latest) [![CI](https://github.com/bwplotka/mdox/workflows/go/badge.svg)](https://github.com/bwplotka/mdox/actions?query=workflow%3Ago) [![Go Report Card](https://goreportcard.com/badge/github.com/bwplotka/mdox)](https://goreportcard.com/report/github.com/bwplotka/mdox)

CLI toolset for maintaining automated, high quality project documentation and website leveraging markdown and git.

Goal: Allow projects to have self-updating up-to-date documentation available in both markdown (e.g readable from GitHub) and static HTML. Hosted in the same repository as code,
fool-proof and integrated with Pull Requests CI and hosting CD.
Goal: Allow projects to have self-updating up-to-date documentation available in both markdown (e.g readable from GitHub) and static HTML. Hosted in the same repository as code, fool-proof and integrated with Pull Requests CI and hosting CD.

### Features

Expand Down Expand Up @@ -44,6 +40,8 @@ Commands:
web gen <files>...
Generate versioned docs
```

### Production Usage
Expand All @@ -67,9 +65,8 @@ or via [bingo](github.com/bwplotka/bingo) if want to pin it:
bingo get -u github.com/bwplotka/mdox
```

Any contributions are welcome! Just use GitHub Issues and Pull Requests as usual.
We follow [Thanos Go coding style](https://thanos.io/contributing/coding-style-guide.md/) guide.
Any contributions are welcome! Just use GitHub Issues and Pull Requests as usual. We follow [Thanos Go coding style](https://thanos.io/contributing/coding-style-guide.md/) guide.

## Initial Author

[@bwplotka](https://bwplotka.dev)
[@bwplotka](https://bwplotka.dev)
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ require (
gopkg.in/alecthomas/kingpin.v2 v2.2.6
)

replace github.com/Kunde21/markdownfmt/v2 => github.com/bwplotka/markdownfmt/v2 v2.0.0-20201027235426-cd85d2653c78
replace github.com/Kunde21/markdownfmt/v2 => github.com/bwplotka/markdownfmt/v2 v2.0.0-20201129164736-749754008490
7 changes: 4 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ github.com/bep/gitmap v1.1.2/go.mod h1:g9VRETxFUXNWzMiuxOwcudo6DfZkW9jOsOW0Ft4kY
github.com/bep/golibsass v0.6.0/go.mod h1:DL87K8Un/+pWUS75ggYv41bliGiolxzDKWJAq3eJ1MA=
github.com/bep/tmc v0.5.1/go.mod h1:tGYHN8fS85aJPhDLgXETVKp+PR382OvFi2+q2GkGsq0=
github.com/bgentry/speakeasy v0.1.0/go.mod h1:+zsyZBPWlz7T6j88CTgSN5bM796AkVf0kBD4zp0CCIs=
github.com/bwplotka/markdownfmt/v2 v2.0.0-20201027235426-cd85d2653c78 h1:Cq42gMQ3fPsTks1ONNXv+s2p6CTh9uWrUd4xJE5EOOI=
github.com/bwplotka/markdownfmt/v2 v2.0.0-20201027235426-cd85d2653c78/go.mod h1:50JNMOFTYtR8g1f+U8BZlw0M9RL5ZUqjOxxTgITeyrg=
github.com/bwplotka/markdownfmt/v2 v2.0.0-20201129164736-749754008490 h1:wP/QJown7dFXZp5N4HCNk39iDWkczn4F8noYqA8Wcbg=
github.com/bwplotka/markdownfmt/v2 v2.0.0-20201129164736-749754008490/go.mod h1:niFn22lPHG2owQ+pHRB0bz3tkrCuVjvlUy4iFdRY+Bo=
github.com/casbin/casbin/v2 v2.1.2/go.mod h1:YcPU1XXisHhLzuxH9coDNf2FbKpjGlbCg3n9yuLkIJQ=
github.com/cenkalti/backoff v2.2.1+incompatible/go.mod h1:90ReRw6GdpyfrHakVjL/QHaoyV4aDUVVkXQJJJ3NXXM=
github.com/census-instrumentation/opencensus-proto v0.2.0/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -266,9 +266,10 @@ github.com/mattn/go-isatty v0.0.4/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNx
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-shellwords v1.0.10 h1:Y7Xqm8piKOO3v10Thp7Z36h4FYFjt5xB//6XvOrs2Gw=
github.com/mattn/go-shellwords v1.0.10/go.mod h1:EZzvwXDESEeg03EKmM+RmDnNOPKG4lLtQsUlTZDWQ8Y=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

package main

import (
Expand Down Expand Up @@ -61,7 +64,7 @@ func main() {

var g run.Group
g.Add(func() error {
// TODO(bwplotka): Move to customised better setup function.
// TODO(bwplotka): Move to customized better setup function.
return runner(ctx, logger)
}, func(err error) {
cancel()
Expand Down Expand Up @@ -110,14 +113,12 @@ This directive runs executable with arguments and put its stderr and stdout outp
Example: mdox fmt *.md
`)
files := cmd.Arg("files", "Markdown file(s) to process.").Required().ExistingFiles()

// TODO(bwplotka): Format markdown files, check and lint links for uniform styles. Adjust links?
cmd.Run(func(ctx context.Context, logger log.Logger) error {
return mdformatter.Format(ctx, logger, *files, mdformatter.WithCodeBlockTransformer(mdgen.NewCodeBlockTransformer()))
})
}

func registerWeb(ctx context.Context, app *extkingpin.App) {
func registerWeb(_ context.Context, app *extkingpin.App) {
cmd := app.Command("web", "Tools for generating static HTML website based on https://gohugo.io/ on every PR with preview")
genCmd := cmd.Command("gen", "Generate versioned docs")

Expand Down
2 changes: 1 addition & 1 deletion pkg/mdformatter/mdformatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ func (f *Formatter) Format(file *os.File, out io.Writer) error {
}
}

// hack: run Convert two times to ensure deterministic whitespace alignment.
// Hack: run Convert two times to ensure deterministic whitespace alignment.
// This also immediately show transformers which are not working well together etc.
tmp := bytes.Buffer{}
if err := gm.Convert(content, &tmp); err != nil {
Expand Down
24 changes: 18 additions & 6 deletions pkg/mdformatter/mdformatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ func TestFormat_FormatSingle_NoTransformers(t *testing.T) {
exp, err := ioutil.ReadFile("testdata/formatted.md")
testutil.Ok(t, err)

t.Run("Formatter not formatted", func(t *testing.T) {
t.Run("Format not formatted", func(t *testing.T) {
buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file, &buf))
testutil.Equals(t, string(exp), buf.String())
})

t.Run("Formatter formatted", func(t *testing.T) {
t.Run("Format formatted", func(t *testing.T) {
file2, err := os.OpenFile("testdata/formatted.md", os.O_RDONLY, 0)
testutil.Ok(t, err)
defer file2.Close()
Expand Down Expand Up @@ -62,10 +62,22 @@ func TestFormat_FormatSingle_Transformers(t *testing.T) {
f := New(context.Background())
f.link = mockLinkTransformer{}

buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file, &buf))

exp, err := ioutil.ReadFile("testdata/formatted_and_transformed.md")
testutil.Ok(t, err)
testutil.Equals(t, string(exp), buf.String())

t.Run("Format not formatted", func(t *testing.T) {
buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file, &buf))
testutil.Equals(t, string(exp), buf.String())
})

t.Run("Format formatted", func(t *testing.T) {
file2, err := os.OpenFile("testdata/formatted_and_transformed.md", os.O_RDONLY, 0)
testutil.Ok(t, err)
defer file2.Close()

buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file2, &buf))
testutil.Equals(t, string(exp), buf.String())
})
}
1 change: 1 addition & 0 deletions pkg/mdformatter/testdata/formatted.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ Flags:
(used as a fallback)
--query.sd-dns-interval=30s
Interval between DNS resolutions.
```

## Configuration
Expand Down
1 change: 1 addition & 0 deletions pkg/mdformatter/testdata/formatted_and_transformed.md
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,7 @@ Flags:
(used as a fallback)
--query.sd-dns-interval=30s
Interval between DNS resolutions.
```

## Configuration
Expand Down
3 changes: 3 additions & 0 deletions pkg/mdformatter/transformer.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

package mdformatter

import (
Expand Down
4 changes: 4 additions & 0 deletions pkg/mdgen/mdgen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

package mdgen

import (
Expand Down Expand Up @@ -87,5 +90,6 @@ func (t *genCodeBlockTransformer) TransformCodeBlock(ctx context.Context, docPat
}

func genGo(ctx context.Context, moduleRoot string, typePath string) ([]byte, error) {
// TODO(bwplotka): To be done.
return nil, nil
}
41 changes: 27 additions & 14 deletions pkg/mdgen/mdgen_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

package mdgen

import (
Expand All @@ -11,19 +14,29 @@ import (
"github.com/bwplotka/mdox/pkg/testutil"
)

func TestEmbed(t *testing.T) {
file, err := os.OpenFile("testdata/embed_in.md", os.O_RDONLY, 0)
func TestFormat_FormatSingle_CodeBlockTransformer(t *testing.T) {
f := mdformatter.New(context.Background(), mdformatter.WithCodeBlockTransformer(NewCodeBlockTransformer()))

exp, err := ioutil.ReadFile("testdata/mdgen_formatted.md")
testutil.Ok(t, err)
defer file.Close()

f := mdformatter.New(context.Background(), mdformatter.WithCodeBlockTransformer(&genCodeBlockTransformer{}))

buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file, &buf))
testutil.Ok(t, ioutil.WriteFile("test.md", buf.Bytes(), os.ModePerm))
//
//exp, err := ioutil.ReadFile("testdata/embed_in.md")
//testutil.Ok(t, err)
//fmt.Println(string(exp))
//testutil.Equals(t, string(exp), buf.String())

t.Run("Format not formatted", func(t *testing.T) {
file, err := os.OpenFile("testdata/mdgen_not_formatted.md", os.O_RDONLY, 0)
testutil.Ok(t, err)
defer file.Close()

buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file, &buf))
testutil.Equals(t, string(exp), buf.String())
})

t.Run("Format formatted", func(t *testing.T) {
file2, err := os.OpenFile("testdata/mdgen_formatted.md", os.O_RDONLY, 0)
testutil.Ok(t, err)
defer file2.Close()

buf := bytes.Buffer{}
testutil.Ok(t, f.Format(file2, &buf))
testutil.Equals(t, string(exp), buf.String())
})
}
3 changes: 3 additions & 0 deletions pkg/mdgen/testdata/cfg.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright (c) Bartłomiej Płotka @bwplotka
// Licensed under the Apache License 2.0.

package testdata

import "github.com/prometheus/common/model"
Expand Down
4 changes: 0 additions & 4 deletions pkg/mdgen/testdata/embed_in_unknown_format.md

This file was deleted.

Loading

0 comments on commit d895183

Please sign in to comment.