2022-02-10 22:06:57 +00:00
|
|
|
|
//go:build !change
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"io/ioutil"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path"
|
|
|
|
|
"reflect"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-04-17 15:28:33 +00:00
|
|
|
|
|
2023-10-03 17:25:41 +00:00
|
|
|
|
"gitlab.com/manytask/itmo-go/public/tools/testtool"
|
2020-02-12 22:25:12 +00:00
|
|
|
|
)
|
|
|
|
|
|
2023-10-03 17:25:41 +00:00
|
|
|
|
const wordcountImportPath = "gitlab.com/manytask/itmo-go/public/wordcount"
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
|
|
var binCache testtool.BinCache
|
|
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
|
|
|
|
os.Exit(func() int {
|
|
|
|
|
var teardown testtool.CloseFunc
|
|
|
|
|
binCache, teardown = testtool.NewBinCache()
|
|
|
|
|
defer teardown()
|
|
|
|
|
|
|
|
|
|
return m.Run()
|
|
|
|
|
}())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestWordCount(t *testing.T) {
|
|
|
|
|
binary, err := binCache.GetBinary(wordcountImportPath)
|
2020-02-16 14:18:51 +00:00
|
|
|
|
require.NoError(t, err)
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
|
|
type counts map[string]int64
|
|
|
|
|
type files []string
|
|
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
files files
|
|
|
|
|
expected map[string]int64
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "empty",
|
|
|
|
|
files: files{``},
|
|
|
|
|
expected: make(counts),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "simple",
|
|
|
|
|
files: files{
|
|
|
|
|
`a
|
|
|
|
|
a
|
|
|
|
|
b
|
2020-02-12 22:42:45 +00:00
|
|
|
|
с
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
a
|
|
|
|
|
b`,
|
|
|
|
|
},
|
|
|
|
|
expected: counts{"a": 3, "b": 2, "": 2},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "multiple-files",
|
|
|
|
|
files: files{
|
|
|
|
|
`a
|
|
|
|
|
a`,
|
|
|
|
|
`a
|
|
|
|
|
b`,
|
|
|
|
|
`b`,
|
|
|
|
|
},
|
|
|
|
|
expected: counts{"a": 3, "b": 2},
|
|
|
|
|
},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
// Create temp directory.
|
|
|
|
|
testDir, err := ioutil.TempDir("", "wordcount-testdata-")
|
2020-02-16 14:18:51 +00:00
|
|
|
|
require.NoError(t, err)
|
2020-02-12 22:25:12 +00:00
|
|
|
|
defer func() { _ = os.RemoveAll(testDir) }()
|
|
|
|
|
|
|
|
|
|
// Create test files in temp directory.
|
|
|
|
|
var files []string
|
|
|
|
|
for _, f := range tc.files {
|
|
|
|
|
file := path.Join(testDir, testtool.RandomName())
|
2023-10-02 19:32:41 +00:00
|
|
|
|
err = ioutil.WriteFile(file, []byte(f), 0o644)
|
2020-02-16 14:18:51 +00:00
|
|
|
|
require.NoError(t, err)
|
2020-02-12 22:25:12 +00:00
|
|
|
|
files = append(files, file)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Run wordcount executable.
|
|
|
|
|
cmd := exec.Command(binary, files...)
|
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
|
|
output, err := cmd.Output()
|
2020-02-16 14:18:51 +00:00
|
|
|
|
require.NoError(t, err)
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
|
|
// Parse output and compare with an expected one.
|
|
|
|
|
counts, err := parseStdout(output)
|
2020-02-16 14:18:51 +00:00
|
|
|
|
require.NoError(t, err)
|
2020-02-12 22:25:12 +00:00
|
|
|
|
|
|
|
|
|
require.True(t, reflect.DeepEqual(tc.expected, counts),
|
|
|
|
|
fmt.Sprintf("expected: %v; got: %v", tc.expected, counts))
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// parseStdout parses wordcount's output of the ['<COUNT>\t<LINE>'] format.
|
|
|
|
|
func parseStdout(data []byte) (map[string]int64, error) {
|
|
|
|
|
counts := make(map[string]int64)
|
|
|
|
|
|
|
|
|
|
for _, line := range strings.Split(string(data), "\n") {
|
|
|
|
|
if line == "" {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
parts := strings.SplitN(line, "\t", 2)
|
|
|
|
|
if len(parts) != 2 {
|
|
|
|
|
return nil, fmt.Errorf("unexpected line format: %s", parts)
|
|
|
|
|
}
|
|
|
|
|
c, err := strconv.ParseInt(parts[0], 10, 64)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("unable to parse line count: %w", err)
|
|
|
|
|
}
|
|
|
|
|
counts[parts[1]] = c
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return counts, nil
|
|
|
|
|
}
|