Skip to content

Commit

Permalink
Changed module name, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
metal3d committed Apr 18, 2022
1 parent f20a033 commit 2716880
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 9 deletions.
52 changes: 46 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ CUR_SHA=$(shell git log -n1 --pretty='%h')
CUR_BRANCH=$(shell git branch --show-current)
VERSION=$(shell git describe --exact-match --tags $(CUR_SHA) 2>/dev/null || echo $(CUR_BRANCH)-$(CUR_SHA))

GIT_PATH:=github.com/metal3d/goreorder
PACKAGE:=$(GIT_PATH)/...
COMMAND_PACKAGE:=$(GIT_PATH)/cmd/goreorder

DIST_CC:=podman run
DIST_CC_OPTS:=--rm -i --userns keep-id -v $(PWD):/go/src/github.com/metal3d/goreorder:z -w /go/src/github.com/metal3d/goreorder -e CGO_ENABLED=0 docker.io/golang:1.18
CC=go
CC_OPTS=-ldflags "-X main.version=$(VERSION)"
PACKAGE:=goreorder/cmd/goreorder

install:
go install -v $(CC_OPTS) $(PACKAGE)
Expand All @@ -17,29 +22,64 @@ uninstall:
dev-build:
go build -v $(CC_OPTS) ./cmd/*.go

.ONESHELL:
dist: clean-dist
mkdir -p dist
$(MAKE) dist/goreorder-linux-amd64
$(MAKE) dist/goreorder-darwin-amd64
$(MAKE) dist/goreorder-windows-amd64.exe
$(MAKE) dist/goreorder-freebsd-amd64
# stripping
strip dist/goreorder-linux-amd64 || true
strip dist/goreorder-darwin-amd64 || true
strip dist/goreorder-windows-amd64.exe || true
strip dist/goreorder-freebsd-amd64 || true
# sign
for i in $$(find dist -type f); do
echo "signing $$i"
echo "sha256: $$(sha256sum $$i)" >> $$i.sha256
done



dist/goreorder-linux-amd64:
GOOS=linux GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@
@mkdir -p dist
ifeq ($(strip $(_CNT)),true)
GOOS=linux GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE)
else
$(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@
endif

dist/goreorder-darwin-amd64:
GOOS=darwin GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@
@mkdir -p dist
ifeq ($(strip $(_CNT)),true)
GOOS=darwin GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE)
else
$(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@
endif

dist/goreorder-windows-amd64.exe:
GOOS=windows GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@
@mkdir -p dist
ifeq ($(strip $(_CNT)),true)
GOOS=windows GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE)
else
$(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@
endif

dist/goreorder-freebsd-amd64:
GOOS=freebsd GOARCH=amd64 $(CC) build $(CC_OPTS) $(PACKAGE) -o $@

@mkdir -p dist
ifeq ($(strip $(_CNT)),true)
GOOS=freebsd GOARCH=amd64 $(CC) build $(CC_OPTS) -o $@ $(COMMAND_PACKAGE)
else
$(DIST_CC) -e _CNT=true $(DIST_CC_OPTS) make $@
endif

clean-dist:
rm -rf dist

clean: clean-dist
rm -f ./goreorder


test:
go test ./ordering -v -cover
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Get release or use `go install github.com/metal3d/goreorder@latest`
If you want to install from source:
```
go install -v github.com/metal3d/goreorder/cmd/
go install -v github.com/metal3d/goreorder/cmd/...
```
You can also get this repository and type:
Expand Down
3 changes: 2 additions & 1 deletion cmd/goreorder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ package main
import (
"flag"
"fmt"
"goreorder/ordering"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"

"github.com/metal3d/goreorder/ordering"
)

var version = "master" // changed at compilation time
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module goreorder
module github.com/metal3d/goreorder

go 1.16
5 changes: 5 additions & 0 deletions ordering/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@ func Parse(filename, formatCommand string) (map[string][]*GoType, map[string][]*
if d.Tok == token.TYPE {
for _, spec := range d.Specs {
if s, ok := spec.(*ast.TypeSpec); ok {
// is it a struct?
if s.Type.(*ast.StructType) == nil {
// no... skip
continue
}
typeDef := &GoType{
Name: s.Name.Name,
OpeningLine: fset.Position(d.Pos()).Line,
Expand Down
122 changes: 122 additions & 0 deletions ordering/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package ordering

import (
"os"
"path/filepath"
"testing"
)

const exampleSourceCode = `package main
// comment 1
// comment 2
type Foo struct {
// comment 3
idfoo int
// comment 4
namefoo string
}
func (b *Bar) BadlyBar(){
print("BadlyBar")
}
// FooMethod1 comment
func (f *Foo) FooMethod1() {
print("FooMethod1")
}
type Bar struct {
// comment 5
idbar int
// comment 6
namebar string
}
func NewFoo() *Foo {
return nil
}
func NewBar() *Bar {
return nil
}
`

const expectedSource = `package main
type Bar struct {
// comment 5
idbar int
// comment 6
namebar string
}
func NewBar() *Bar {
return nil
}
func (b *Bar) BadlyBar() {
print("BadlyBar")
}
// comment 1
// comment 2
type Foo struct {
// comment 3
idfoo int
// comment 4
namefoo string
}
func NewFoo() *Foo {
return nil
}
// FooMethod1 comment
func (f *Foo) FooMethod1() {
print("FooMethod1")
}
`

func setup() string {
// write exampleSourceCode in a temporary file and return the filename
dirname, err := os.MkdirTemp("", "goreorder-")
if err != nil {
panic(err)
}
filename := filepath.Join(dirname, "example.go")
file, err := os.Create(filename)
if err != nil {
panic(err)
}
defer file.Close()
_, err = file.WriteString(exampleSourceCode)
if err != nil {
panic(err)
}

return filename

}

func teardown(filename string) {
// remove the temporary file
os.Remove(filename)
}

func TestReorder(t *testing.T) {
filename := setup()
defer teardown(filename)

// reorder the file
content, err := ReorderSource(filename, "gofmt", true)
if err != nil {
t.Error(err)
}

// check the content
if content != expectedSource {
t.Errorf("Expected:\n%s\nGot:\n%s\n", expectedSource, content)
}

}

0 comments on commit 2716880

Please sign in to comment.