5 Commits
11 changed files with 26 additions and 408 deletions

No files matched your search

+3 -9
View File
@@ -10,7 +10,6 @@ import (
"path/filepath" "path/filepath"
"runtime/debug" "runtime/debug"
"strings" "strings"
"text/tabwriter"
) )
func getVersion() string { func getVersion() string {
@@ -141,24 +140,19 @@ func main() {
} }
fmt.Printf("\n\nSUMMARY\n\n") fmt.Printf("\n\nSUMMARY\n\n")
w := tabwriter.NewWriter(os.Stdout, 0, 0, 2, ' ', 0) fmt.Printf("%-60s %-10s %-10s %-10s %-10s %-10s %-10s\n\n", "SUITE", "COUNT", "PASSED", "FAILURES", "ERRORS", "DISABLED", "SKIPPED")
fmt.Fprintf(w, "%s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\t%-10s\n",
"SUITE", "COUNT", "PASSED", "FAILURES", "ERRORS", "DISABLED", "SKIPPED")
for _, suite := range testsuites.Suites { for _, suite := range testsuites.Suites {
fmt.Fprintf(w, "%s\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\n", fmt.Printf("%-60s %-10d %-10d %-10d %-10d %-10d %-10d\n",
suite.Name, suite.Name,
suite.TestCount, suite.TestCount,
suite.TestCount-suite.Failures-suite.Errors-suite.Skipped-suite.Disabled, suite.TestCount-suite.Failures-suite.Errors-suite.Skipped-suite.Disabled,
suite.Failures, suite.Errors, suite.Disabled, suite.Skipped) suite.Failures, suite.Errors, suite.Disabled, suite.Skipped)
} }
fmt.Fprintf(w, "%s\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\t%-10d\n", fmt.Printf("\n%-60s %-10d %-10d %-10d %-10d %-10d %-10d\n",
"TOTAL", "TOTAL",
testsuites.Tests, testsuites.Tests,
testsuites.Tests-testsuites.Failures-testsuites.Errors-testsuites.Skipped-testsuites.Disabled, testsuites.Tests-testsuites.Failures-testsuites.Errors-testsuites.Skipped-testsuites.Disabled,
testsuites.Failures, testsuites.Errors, testsuites.Disabled, testsuites.Skipped) testsuites.Failures, testsuites.Errors, testsuites.Disabled, testsuites.Skipped)
w.Flush()
if testsuites.Failures+testsuites.Errors+testsuites.Skipped+testsuites.Disabled > 0 { if testsuites.Failures+testsuites.Errors+testsuites.Skipped+testsuites.Disabled > 0 {
fmt.Printf("\nFAILED TESTS\n\n") fmt.Printf("\nFAILED TESTS\n\n")
-137
View File
@@ -1,137 +0,0 @@
package main
import (
"fmt"
"math/rand"
"time"
)
// from: https://github.com/peter-evans/patience/blob/main/lcs.go
func LCS[T comparable](a, b []T, equals func(T, T) bool) [][2]int {
// Initialize the LCS table.
lcs := make([][]int, len(a)+1)
for i := 0; i <= len(a); i++ {
lcs[i] = make([]int, len(b)+1)
}
// Populate the LCS table.
for i := 1; i < len(lcs); i++ {
for j := 1; j < len(lcs[i]); j++ {
if equals(a[i-1], b[j-1]) {
lcs[i][j] = lcs[i-1][j-1] + 1
} else {
lcs[i][j] = max(lcs[i-1][j], lcs[i][j-1])
}
}
}
// Backtrack to find the LCS.
i, j := len(a), len(b)
s := make([][2]int, 0, lcs[i][j])
for i > 0 && j > 0 {
switch {
case equals(a[i-1], b[j-1]):
s = append(s, [2]int{i - 1, j - 1})
i--
j--
case lcs[i-1][j] > lcs[i][j-1]:
i--
default:
j--
}
}
// Reverse the backtracked LCS.
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
func LCSDiff[T comparable](a, b []T, equals func(T, T) bool,
diff func(T), same func(T, T)) {
res := LCS(a, b, equals)
i2 := 0
isame := 0
for isame < len(res) {
// process [i1..isamee1[ and [i2..isame2[
for i2 < res[isame][1] {
diff(b[i2])
i2++
}
// process same elements -> no diff so normally not
same(a[res[isame][0]], b[res[isame][1]])
i2++
isame++
}
for i2 < len(b) {
diff(b[i2])
i2++
}
}
const letters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
func randString(n int) string {
res := make([]byte, n)
for i := range n {
res[i] = letters[rand.Int()%len(letters)]
}
return string(res)
}
func main() {
input1 := []string{"ab", "cd", "myidenticalstring"}
input2 := []string{"ab", "ce", "myidenticalstrings", "fg"}
t0 := time.Now()
res := LCS(input1, input2, func(s1 string, s2 string) bool {
res := LCS([]rune(s1), []rune(s2), func(r rune, r2 rune) bool {
return r == r2
})
score := min(float64(len(res))/float64(len(s1)),
float64(len(res))/float64(len(s2)))
return score > 0.90
})
dt := time.Now().Sub(t0).Microseconds()
fmt.Printf("time %v us, len %v\n", dt, len(res))
for i, pair := range res {
fmt.Printf("%d: %v\n", i, pair)
}
// [0..i1[ and [0..i2[ already handled
// isame: res[0..isame[ have been handled
//i1 := 0
i2 := 0
isame := 0
for isame < len(res) {
// process [i1..isamee1[ and [i2..isame2[
for i2 < res[isame][1] {
fmt.Printf("DIFF|ADD %s\n", input2[i2])
i2++
}
// process same elements -> no diff so normally not
fmt.Printf("SAME %s %s\n", input1[res[isame][0]], input2[res[isame][1]])
i2++
isame++
}
for i2 < len(input2) {
fmt.Printf("DIFF|ADD %s\n", input2[i2])
i2++
}
LCSDiff(input1, input2, func(s1 string, s2 string) bool {
res := LCS([]rune(s1), []rune(s2), func(r rune, r2 rune) bool {
return r == r2
})
score := min(float64(len(res))/float64(len(s1)),
float64(len(res))/float64(len(s2)))
return score > 0.90
}, func(s string) {
fmt.Printf("DIFF %v\n", s)
}, func(s1 string, s2 string) {
fmt.Printf("SAME %v %v\n", s1, s2)
})
}
+7 -38
View File
@@ -2,11 +2,10 @@ package main
import ( import (
"fmt" "fmt"
"os"
"reflect"
"github.com/goccy/go-yaml" "github.com/goccy/go-yaml"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os"
"reflect"
) )
// hack to be able to compare slices and dictionires that cannot be put into a map. // hack to be able to compare slices and dictionires that cannot be put into a map.
@@ -46,13 +45,7 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
v1set[strval(v)] = true v1set[strval(v)] = true
} }
s := make([]any, 0) s := make([]any, 0)
// TODO for k2, _ := range v2set {
// convert both slices to lists of strings
// apply LCS to the list of strings with approximate equality
// added elements: -> output fully
// approximately equal elements: -> when identical, skip, otherwise, output diffs (recurse)
for _, v2value := range v2.([]any) {
k2 := strval(v2value)
if v1set[k2] { if v1set[k2] {
if VERBOSITY == 2 { if VERBOSITY == 2 {
s = append(s, "<UNMODIFIED>") s = append(s, "<UNMODIFIED>")
@@ -74,15 +67,6 @@ func subtract(yaml2 yaml.MapSlice, yaml1 yaml.MapSlice) yaml.MapSlice {
return res return res
} }
func compare(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) (yaml.MapSlice, yaml.MapSlice) {
((MapSlice)(yaml1)).Sort()
((MapSlice)(yaml2)).Sort()
diff1 := subtract(yaml2, yaml1)
diff2 := subtract(yaml1, yaml2)
return diff1, diff2
}
func diff(cmd *cobra.Command, args []string) error { func diff(cmd *cobra.Command, args []string) error {
if len(args) != 2 { if len(args) != 2 {
return fmt.Errorf("Expected 2 files") return fmt.Errorf("Expected 2 files")
@@ -93,28 +77,13 @@ func diff(cmd *cobra.Command, args []string) error {
file1 := args[0] file1 := args[0]
file2 := args[1] file2 := args[1]
data1, err := read(file1) yaml1 := parse(read(file1))
if err != nil { yaml2 := parse(read(file2))
return err
}
data2, err := read(file2)
if err != nil {
return err
}
yaml1, err := parse(data1) diff1 := subtract(yaml2, yaml1)
if err != nil {
return fmt.Errorf("%s: %w", file1, err)
}
yaml2, err := parse(data2)
if err != nil {
return fmt.Errorf("%s: %w", file2, err)
}
diff1, backwardDiff := compare(yaml1, yaml2)
diff2 := make(yaml.MapSlice, 0) diff2 := make(yaml.MapSlice, 0)
if SYMMETRIC_DIFF { if SYMMETRIC_DIFF {
diff2 = backwardDiff diff2 = subtract(yaml1, yaml2)
} }
diff := diff1 diff := diff1
-76
View File
@@ -1,76 +0,0 @@
package main
import (
"testing"
"github.com/goccy/go-yaml"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type DiffSuite struct {
suite.Suite
}
func TestDiffSuite(t *testing.T) {
suite.Run(t, new(DiffSuite))
}
func (s *DiffSuite) TestCompareIgnoresTopLevelMapKeyOrder() {
diff1, diff2 := s.compareDocuments(`
first: 1
second: 2
`, `
second: 2
first: 1
`)
require.Empty(s.T(), diff1)
require.Empty(s.T(), diff2)
}
func (s *DiffSuite) TestCompareIgnoresMapKeyOrderInArray() {
diff1, diff2 := s.compareDocuments(`
items:
- name: alpha
value: 1
- name: beta
value: 2
`, `
items:
- value: 1
name: alpha
- value: 2
name: beta
`)
require.Empty(s.T(), diff1)
require.Empty(s.T(), diff2)
}
func (s *DiffSuite) TestCompareStillDetectsChangedMapValue() {
diff1, diff2 := s.compareDocuments(`
items:
- name: alpha
value: 1
`, `
items:
- value: 2
name: alpha
`)
require.NotEmpty(s.T(), diff1)
require.NotEmpty(s.T(), diff2)
}
func (s *DiffSuite) compareDocuments(yaml1Data, yaml2Data string) (yaml.MapSlice, yaml.MapSlice) {
s.T().Helper()
yaml1, err := parse([]byte(yaml1Data))
require.NoError(s.T(), err)
yaml2, err := parse([]byte(yaml2Data))
require.NoError(s.T(), err)
diff1, diff2 := compare(yaml1, yaml2)
return diff1, diff2
}
+3 -12
View File
@@ -1,12 +1,10 @@
package main package main
import ( import (
"fmt"
"os"
"reflect"
"github.com/goccy/go-yaml" "github.com/goccy/go-yaml"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"os"
"reflect"
) )
type MyMap yaml.MapSlice type MyMap yaml.MapSlice
@@ -55,14 +53,7 @@ func mergeMap(yaml1 yaml.MapSlice, yaml2 yaml.MapSlice) yaml.MapSlice {
func merge(cmd *cobra.Command, args []string) error { func merge(cmd *cobra.Command, args []string) error {
res := make(yaml.MapSlice, 0) res := make(yaml.MapSlice, 0)
for _, arg := range args { for _, arg := range args {
data, err := read(arg) config := parse(read(arg))
if err != nil {
return err
}
config, err := parse(data)
if err != nil {
return fmt.Errorf("%s: %w", arg, err)
}
res = mergeMap(res, config) res = mergeMap(res, config)
} }
encode(os.Stdout, res) encode(os.Stdout, res)
+12 -25
View File
@@ -2,38 +2,25 @@ package main
import ( import (
"bytes" "bytes"
"fmt"
"os"
"github.com/goccy/go-yaml" "github.com/goccy/go-yaml"
"github.com/spf13/cobra" "os"
) )
func parse(data []byte) (yaml.MapSlice, error) { func read(file string) []byte {
data, err := os.ReadFile(file)
if err != nil {
panic(err)
}
return data
}
func parse(data []byte) yaml.MapSlice {
var result yaml.MapSlice var result yaml.MapSlice
decoder := yaml.NewDecoder(bytes.NewReader(data), decoder := yaml.NewDecoder(bytes.NewReader(data),
yaml.UseOrderedMap()) yaml.UseOrderedMap())
err := decoder.Decode(&result) err := decoder.Decode(&result)
if err != nil { if err != nil {
return nil, err panic(err)
} }
return result, nil return result
}
func parseFiles(cmd *cobra.Command, args []string) error {
for _, arg := range args {
data, err := read(arg)
if err != nil {
return err
}
decoded, err := parse(data)
if err != nil {
fmt.Printf("%s: %v\n", arg, err.Error())
}
if len(args) > 1 {
fmt.Printf("---\n")
}
encode(os.Stdout, decoded)
}
return nil
} }
-55
View File
@@ -1,55 +0,0 @@
package main
import (
"cmp"
"fmt"
"reflect"
"slices"
"github.com/goccy/go-yaml"
"github.com/spf13/cobra"
"os"
)
type MapSlice yaml.MapSlice
func (s MapSlice) Sort() {
slices.SortFunc(s, func(a, b yaml.MapItem) int {
keya := fmt.Sprintf("%s", a.Key)
keyb := fmt.Sprintf("%s", b.Key)
return cmp.Compare(keya, keyb)
})
for _, item := range s {
switch {
case reflect.TypeOf(item.Value) == reflect.TypeOf(yaml.MapSlice{}):
((MapSlice)(item.Value.(yaml.MapSlice))).Sort()
case Type(item.Value) == Slice:
for _, v := range item.Value.([]any) {
ms, ok := v.(yaml.MapSlice)
if ok {
((MapSlice)(ms)).Sort()
}
}
}
}
}
func sortYaml(cmd *cobra.Command, args []string) error {
for _, arg := range args {
data, err := read(arg)
if err != nil {
return err
}
doc, err := parse(data)
if err != nil {
return fmt.Errorf("%s: %v\n", arg, err.Error())
}
((MapSlice)(doc)).Sort()
if len(args) > 1 {
fmt.Printf("---\n")
}
encode(os.Stdout, doc)
}
return nil
}
-23
View File
@@ -1,23 +0,0 @@
package main
import (
"fmt"
"io"
"os"
)
func read(file string) ([]byte, error) {
if file == "-" {
data, err := io.ReadAll(os.Stdin)
if err != nil {
return nil, fmt.Errorf("Error reading from stdin")
}
return data, nil
}
data, err := os.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Error reading from '%s'", file)
}
return data, nil
}
-20
View File
@@ -57,26 +57,6 @@ Shows the additions and modifications in <file2> compared to <file1>`,
} }
cmd.AddCommand(merge) cmd.AddCommand(merge)
parse := &cobra.Command{
Use: "parse [file1] ... [fileN]",
Short: "Parse yaml files.",
Long: `Parse yaml files, usually gives better error messages than yamllint or yq`,
RunE: func(cmd *cobra.Command, args []string) error {
return parseFiles(cmd, args)
},
}
cmd.AddCommand(parse)
sort := &cobra.Command{
Use: "sort [file1] ... [fileN]",
Short: "Sort the yaml output by sorting based on map key ",
Long: `Sort yaml files, this makes it easier to also use regular diff`,
RunE: func(cmd *cobra.Command, args []string) error {
return sortYaml(cmd, args)
},
}
cmd.AddCommand(sort)
diff.PersistentFlags().IntVarP(&VERBOSITY, "array-output-level", diff.PersistentFlags().IntVarP(&VERBOSITY, "array-output-level",
"v", 3, `Array output level: , "v", 3, `Array output level: ,
0: no output, only exit status, 0: no output, only exit status,
+1 -5
View File
@@ -1,17 +1,13 @@
module git.wamblee.org/public/gotools module git.wamblee.org/public/gotools
go 1.24.7 go 1.23.4
require ( require (
github.com/goccy/go-yaml v1.15.13 github.com/goccy/go-yaml v1.15.13
github.com/spf13/cobra v1.8.1 github.com/spf13/cobra v1.8.1
github.com/stretchr/testify v1.10.0
) )
require ( require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect github.com/spf13/pflag v1.0.5 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
) )
-8
View File
@@ -1,20 +1,12 @@
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/goccy/go-yaml v1.15.13 h1:Xd87Yddmr2rC1SLLTm2MNDcTjeO/GYo0JGiww6gSTDg= github.com/goccy/go-yaml v1.15.13 h1:Xd87Yddmr2rC1SLLTm2MNDcTjeO/GYo0JGiww6gSTDg=
github.com/goccy/go-yaml v1.15.13/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA= github.com/goccy/go-yaml v1.15.13/go.mod h1:XBurs7gK8ATbW4ZPGKgcbrY1Br56PdM69F7LkFRi1kA=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8= github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw= github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM= github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y= github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=