Impl versions with shell commands
parent
217a221cf4
commit
fecd343f1b
|
|
@ -0,0 +1,65 @@
|
|||
package versions
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"local/notes-server/config"
|
||||
"os"
|
||||
"path"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestVersionsHappy(t *testing.T) {
|
||||
d, err := ioutil.TempDir(os.TempDir(), "prefix")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
if err := ioutil.WriteFile(path.Join(d, "a.md"), []byte("# Hello"), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(d, "b.md"), []byte("# World"), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config.Root = d
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
if err := v.AddAll(); err != nil {
|
||||
t.Error("failed add", err)
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
t.Error("failed commit", err)
|
||||
}
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
t.Error("failed gitmmit", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionsBad(t *testing.T) {
|
||||
config.Root = "/not/a/real/path"
|
||||
if _, err := New(); err == nil {
|
||||
t.Error("passed new from nil path")
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionsDirty(t *testing.T) {
|
||||
if os.Getenv("DIRTY") == "" {
|
||||
return
|
||||
}
|
||||
config.Root = "/tmp/foo"
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ioutil.WriteFile(path.Join(config.Root, "file.md"), []byte(`
|
||||
# Hello
|
||||
## World
|
||||
I'm a doc
|
||||
`), os.ModePerm)
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
t.Log(v)
|
||||
}
|
||||
|
|
@ -1,6 +1,8 @@
|
|||
package versions
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"local/notes-server/config"
|
||||
"os/exec"
|
||||
"time"
|
||||
)
|
||||
|
|
@ -9,25 +11,32 @@ type Versions struct {
|
|||
}
|
||||
|
||||
func New() (*Versions, error) {
|
||||
return &Versions{}, nil
|
||||
v := &Versions{}
|
||||
v.cmd("git", "init")
|
||||
return v, nil
|
||||
}
|
||||
|
||||
func (v *Versions) Gitmmit() error {
|
||||
if err := v.AddAll(); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("cannot add all: %v", err)
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
return err
|
||||
return fmt.Errorf("cannot commit: %v", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (v *Versions) AddAll() error {
|
||||
cmd := exec.Command("git", "add", "-A", ":/")
|
||||
return cmd.Run()
|
||||
return v.cmd("git", "add", "-A", ":/")
|
||||
}
|
||||
|
||||
func (v *Versions) Commit() error {
|
||||
cmd := exec.Command("git", "commit", "-m", time.Now().String())
|
||||
return cmd.Run()
|
||||
return v.cmd("git", "commit", "-m", time.Now().String())
|
||||
}
|
||||
|
||||
func (v *Versions) cmd(cmd string, args ...string) error {
|
||||
command := exec.Command(cmd, args...)
|
||||
command.Dir = config.Root
|
||||
_, err := command.CombinedOutput()
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,58 +8,35 @@ import (
|
|||
"testing"
|
||||
)
|
||||
|
||||
func TestVersionsHappy(t *testing.T) {
|
||||
func TestVersions(t *testing.T) {
|
||||
d, err := ioutil.TempDir(os.TempDir(), "prefix")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(d)
|
||||
if err := ioutil.WriteFile(path.Join(d, "a.md"), []byte("# Hello"), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := ioutil.WriteFile(path.Join(d, "b.md"), []byte("# World"), os.ModePerm); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
config.Root = d
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, f := range []string{"a", "b"} {
|
||||
ioutil.WriteFile(path.Join(d, f+".md"), []byte(f), os.ModePerm)
|
||||
}
|
||||
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := v.AddAll(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := v.Commit(); err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
|
||||
if err := v.Gitmmit(); err == nil {
|
||||
t.Error(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionsBad(t *testing.T) {
|
||||
config.Root = "/not/a/real/path"
|
||||
if _, err := New(); err == nil {
|
||||
t.Error(err)
|
||||
for _, f := range []string{"c", "b"} {
|
||||
ioutil.WriteFile(path.Join(d, f+".md"), []byte("d"), os.ModePerm)
|
||||
}
|
||||
}
|
||||
|
||||
func TestVersionsDirty(t *testing.T) {
|
||||
if os.Getenv("DIRTY") == "" {
|
||||
return
|
||||
}
|
||||
config.Root = "/tmp/foo"
|
||||
v, err := New()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ioutil.WriteFile(path.Join(config.Root, "file.md"), []byte(`
|
||||
# Hello
|
||||
## World
|
||||
I'm a doc
|
||||
`), os.ModePerm)
|
||||
if err := v.Gitmmit(); err != nil {
|
||||
t.Fatal(err)
|
||||
t.Error(config.Root, err)
|
||||
}
|
||||
t.Log(v)
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue