2014-07-26 18:18:29 +02:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
// Make the test files from fstests.go
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"html/template"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Search fstests.go and return all the test function names
|
|
|
|
func findTestFunctions() []string {
|
|
|
|
fns := []string{}
|
|
|
|
matcher := regexp.MustCompile(`^func\s+(Test.*?)\(`)
|
|
|
|
|
|
|
|
in, err := os.Open("fstests.go")
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Couldn't open fstests.go: %v", err)
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
scanner := bufio.NewScanner(in)
|
|
|
|
for scanner.Scan() {
|
|
|
|
line := scanner.Text()
|
|
|
|
matches := matcher.FindStringSubmatch(line)
|
|
|
|
if len(matches) > 0 {
|
|
|
|
fns = append(fns, matches[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
|
|
log.Fatalf("Error scanning file: %v", err)
|
|
|
|
}
|
|
|
|
return fns
|
|
|
|
}
|
|
|
|
|
|
|
|
// Data to substitute
|
|
|
|
type Data struct {
|
2017-08-01 14:38:36 +02:00
|
|
|
Regenerate string
|
|
|
|
FsName string
|
|
|
|
UpperFsName string
|
|
|
|
TestName string
|
|
|
|
Fns []string
|
|
|
|
Suffix string
|
|
|
|
BuildConstraint string
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var testProgram = `
|
|
|
|
// Test {{ .UpperFsName }} filesystem interface
|
|
|
|
//
|
|
|
|
// Automatically generated - DO NOT EDIT
|
2017-08-01 14:38:36 +02:00
|
|
|
// Regenerate with: {{ .Regenerate }}{{ if ne .BuildConstraint "" }}
|
|
|
|
|
|
|
|
// +build {{ .BuildConstraint }}
|
|
|
|
{{end}}
|
2014-07-26 18:18:29 +02:00
|
|
|
package {{ .FsName }}_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ncw/rclone/fs"
|
|
|
|
"github.com/ncw/rclone/fstest/fstests"
|
|
|
|
"github.com/ncw/rclone/{{ .FsName }}"
|
2016-07-25 20:18:56 +02:00
|
|
|
{{ if eq .FsName "crypt" }} _ "github.com/ncw/rclone/local"
|
|
|
|
{{end}})
|
2014-07-26 18:18:29 +02:00
|
|
|
|
2016-08-20 19:47:33 +02:00
|
|
|
func TestSetup{{ .Suffix }}(t *testing.T)() {
|
2015-11-07 12:14:46 +01:00
|
|
|
fstests.NilObject = fs.Object((*{{ .FsName }}.Object)(nil))
|
2014-07-26 18:18:29 +02:00
|
|
|
fstests.RemoteName = "{{ .TestName }}"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generic tests for the Fs
|
2016-08-20 19:47:33 +02:00
|
|
|
{{ range $fn := .Fns }}func {{ $fn }}{{ $.Suffix }}(t *testing.T){ fstests.{{ $fn }}(t) }
|
2014-07-26 18:18:29 +02:00
|
|
|
{{ end }}
|
|
|
|
`
|
|
|
|
|
2017-08-01 14:38:36 +02:00
|
|
|
// options for generateTestProgram
|
|
|
|
type (
|
|
|
|
suffix string
|
|
|
|
buildConstraint string
|
|
|
|
)
|
2014-07-26 18:18:29 +02:00
|
|
|
|
2017-08-01 14:38:36 +02:00
|
|
|
// Generate test file piping it through gofmt
|
|
|
|
func generateTestProgram(t *template.Template, fns []string, Fsname string, options ...interface{}) {
|
2014-07-26 18:18:29 +02:00
|
|
|
data := Data{
|
2015-02-14 19:48:08 +01:00
|
|
|
Regenerate: "make gen_tests",
|
2017-08-01 14:38:36 +02:00
|
|
|
FsName: strings.ToLower(Fsname),
|
2014-07-26 18:18:29 +02:00
|
|
|
UpperFsName: Fsname,
|
|
|
|
Fns: fns,
|
2017-08-01 14:38:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, option := range options {
|
|
|
|
switch x := option.(type) {
|
|
|
|
case suffix:
|
|
|
|
data.Suffix = string(x)
|
|
|
|
case buildConstraint:
|
|
|
|
data.BuildConstraint = string(x)
|
|
|
|
default:
|
|
|
|
log.Fatalf("Unknown option type %T", option)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data.TestName = "Test" + data.UpperFsName + data.Suffix + ":"
|
|
|
|
outfile := "../../" + data.FsName + "/" + data.FsName + data.Suffix + "_test.go"
|
|
|
|
|
|
|
|
if data.FsName == "local" {
|
|
|
|
data.TestName = ""
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cmd := exec.Command("gofmt")
|
|
|
|
|
|
|
|
log.Printf("Writing %q", outfile)
|
|
|
|
out, err := os.Create(outfile)
|
|
|
|
if err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to write %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
cmd.Stdout = out
|
|
|
|
|
|
|
|
gofmt, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to StdinPipe %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
if err = cmd.Start(); err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to Start %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
if err = t.Execute(gofmt, data); err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to Execute %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
if err = gofmt.Close(); err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to Close gofmt on %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
if err = cmd.Wait(); err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to Wait %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
if err = out.Close(); err != nil {
|
2017-08-01 14:38:36 +02:00
|
|
|
log.Fatalf("Failed to Close out %q: %v", outfile, err)
|
2014-07-26 18:18:29 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
fns := findTestFunctions()
|
|
|
|
t := template.Must(template.New("main").Parse(testProgram))
|
2017-08-01 14:38:36 +02:00
|
|
|
generateTestProgram(t, fns, "Local")
|
|
|
|
generateTestProgram(t, fns, "Swift")
|
|
|
|
generateTestProgram(t, fns, "S3")
|
|
|
|
generateTestProgram(t, fns, "Drive")
|
|
|
|
generateTestProgram(t, fns, "GoogleCloudStorage")
|
|
|
|
generateTestProgram(t, fns, "Dropbox")
|
|
|
|
generateTestProgram(t, fns, "AmazonCloudDrive")
|
|
|
|
generateTestProgram(t, fns, "OneDrive")
|
|
|
|
generateTestProgram(t, fns, "Hubic")
|
|
|
|
generateTestProgram(t, fns, "B2")
|
|
|
|
generateTestProgram(t, fns, "Yandex")
|
|
|
|
generateTestProgram(t, fns, "Crypt")
|
|
|
|
generateTestProgram(t, fns, "Crypt", suffix("2"))
|
|
|
|
generateTestProgram(t, fns, "Crypt", suffix("3"))
|
|
|
|
generateTestProgram(t, fns, "Sftp")
|
|
|
|
generateTestProgram(t, fns, "FTP")
|
|
|
|
generateTestProgram(t, fns, "Box")
|
2017-06-26 01:47:54 +02:00
|
|
|
generateTestProgram(t, fns, "QingStor")
|
2014-07-26 18:18:29 +02:00
|
|
|
log.Printf("Done")
|
|
|
|
}
|