Skip to content

Commit

Permalink
add experimental staging support
Browse files Browse the repository at this point in the history
  • Loading branch information
powerman committed Apr 16, 2016
1 parent a004a12 commit 2b79792
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
3 changes: 3 additions & 0 deletions narada/staging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package narada

import _ "github.com/powerman/narada-go/narada/staging"
124 changes: 124 additions & 0 deletions narada/staging/staging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
// Package staging provides temporary Narada project directory for tests.
// It must be imported in first import statement by all files of your package
// (including non-test files) which imports any narada-aware package,
// even before narada/bootstrap import in main package.
//
// Importing this package will have effect only under `go test`:
// current directory will be changed to temporary Narada project directory.
// To cleanup that directory after tests call TearDown like this:
//
// func TestMain(m *testing.M) { os.Exit(staging.TearDown(m.Run())) }
package staging

import (
"flag"
"io/ioutil"
"log"
"os"
"os/exec"
)

var (
pkgDir string
tmpDir string
dirs = []string{
".backup",
"config",
"config/backup",
"config/log",
"config/mysql",
"config/mysql/dump",
"config/qmail",
"tmp",
"var",
"var/log",
"var/use",
"var/mysql",
"var/qmail",
}
files = []struct{ name, data string }{
{"config/backup/exclude", "./.backup/*\n./.lock*\n./tmp/*\n./.release/*\n"},
{"config/log/level", "DEBUG"},
{"config/log/type", "file"},
{"config/log/file", "/dev/stdout"},
{"config/mysql/host", ""},
{"config/mysql/port", "3306"},
{"config/mysql/db", ""},
{"config/mysql/login", ""},
{"config/mysql/pass", ""},
{"config/mysql/dump/empty", ""},
{"config/mysql/dump/ignore", ""},
{"config/mysql/dump/incremental", ""},
}
)

func init() {
if flag.Lookup("test.v") != nil {
err := setUp()
if err != nil {
log.Fatal(err)
}
}
}

func setUp() (err error) {
pkgDir, err = os.Getwd()
if err != nil {
return err
}
tmpDir, err = ioutil.TempDir("", "narada-staging.")
if err != nil {
return err
}
err = os.Chdir(tmpDir)
if err != nil {
return err
}

for _, dir := range dirs {
err = os.Mkdir(dir, 0777)
if err != nil {
return err
}
}
for _, file := range files {
err = ioutil.WriteFile(file.name, []byte(file.data), 0666)
if err != nil {
return err
}
}

custom := pkgDir + "/testdata/narada.setup"
if _, err = os.Stat(custom); err == nil {
err = exec.Command(custom, pkgDir).Run()
if err != nil {
return err
}
}

return nil
}

func TearDown(exitCode int) int {
var err error
custom := pkgDir + "/testdata/narada.teardown"
if _, err = os.Stat(custom); err == nil {
err = exec.Command(custom, pkgDir).Run()
if err != nil {
log.Print(err)
return exitCode
}
}

err = os.Chdir(pkgDir)
if err != nil {
log.Print(err)
return exitCode
}
err = os.RemoveAll(tmpDir)
if err != nil {
log.Print(err)
return exitCode
}
return exitCode
}

0 comments on commit 2b79792

Please sign in to comment.