Skip to content
This repository has been archived by the owner on Jul 7, 2020. It is now read-only.

Commit

Permalink
Spec.ReadFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi committed Feb 24, 2013
1 parent 19f70c7 commit 7e587e6
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -4,6 +4,7 @@
+ `nut get` now downloads and installs all dependencies (recursive).
+ `nut get` now may be used without arguments.
* `nut pack` now includes files for all OSes.
+ New API for Spec: ReadFile().
+ New API for Nut: ReadFrom(), FilePath(), ImportPath().
* Fix bug with `nut get` and www.-prefixed hosts.
* Fix some issues on Windows.
Expand Down
21 changes: 20 additions & 1 deletion spec.go
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"net/url"
"os"
"regexp"
"strings"
)
Expand Down Expand Up @@ -33,6 +34,24 @@ const (

var VendorRegexp = regexp.MustCompile(`^[0-9A-Za-z_]+$`)

// check interface
var (
_ io.ReaderFrom = &Spec{}
_ io.WriterTo = &Spec{}
)

// Reads spec from specified file.
func (spec *Spec) ReadFile(fileName string) (err error) {
f, err := os.Open(fileName)
if err != nil {
return
}
defer f.Close()

_, err = spec.ReadFrom(f)
return
}

// ReadFrom reads spec from r until EOF.
// The return value n is the number of bytes read.
// Any error except io.EOF encountered during the read is also returned.
Expand Down Expand Up @@ -66,7 +85,7 @@ func (spec *Spec) WriteTo(w io.Writer) (n int64, err error) {
return
}

// Check spec for errors and return them.
// Checks spec for errors and return them.
func (spec *Spec) Check() (errors []string) {
// check version
if spec.Version.String() == "0.0.0" {
Expand Down
47 changes: 26 additions & 21 deletions spec_test.go
Expand Up @@ -10,7 +10,7 @@ import (
)

type S struct {
f *os.File
s *Spec
b *bytes.Buffer
}

Expand All @@ -19,34 +19,39 @@ var _ = Suite(&S{})
func (f *S) SetUpTest(c *C) {
file, err := os.Open("../test_nut1/nut.json")
c.Assert(err, IsNil)
f.f = file
defer file.Close()

b, err := ioutil.ReadAll(f.f)
b, err := ioutil.ReadAll(file)
c.Assert(err, IsNil)
f.b = bytes.NewBuffer(b)
file.Seek(0, 0)

_, err = f.f.Seek(0, 0)
s := new(Spec)
n, err := s.ReadFrom(file)
c.Assert(err, IsNil)
c.Assert(n, Equals, int64(f.b.Len()))
f.s = s
}

func (f *S) TestReadWrite(c *C) {
spec := new(Spec)

n, err := spec.ReadFrom(f.f)
c.Check(n, Equals, int64(f.b.Len()))
c.Assert(err, IsNil)

c.Check(spec.Version.String(), Equals, "0.0.1")
c.Check(spec.Vendor, Equals, "debug")
c.Check(len(spec.Authors), Equals, 1)
c.Check(spec.Authors[0], Equals, Person{FullName: "Alexey Palazhchenko", Email: "alexey.palazhchenko@gmail.com"})
c.Check(len(spec.ExtraFiles), Equals, 2)
c.Check(spec.ExtraFiles[0], Equals, "README")
c.Check(spec.ExtraFiles[1], Equals, "LICENSE")
func (f *S) TestReadFromWriteTo(c *C) {
c.Check(f.s.Version.String(), Equals, "0.0.1")
c.Check(f.s.Vendor, Equals, "debug")
c.Check(len(f.s.Authors), Equals, 1)
c.Check(f.s.Authors[0], Equals, Person{FullName: "Alexey Palazhchenko", Email: "alexey.palazhchenko@gmail.com"})
c.Check(len(f.s.ExtraFiles), Equals, 2)
c.Check(f.s.ExtraFiles[0], Equals, "README")
c.Check(f.s.ExtraFiles[1], Equals, "LICENSE")

buf := new(bytes.Buffer)
n2, err := spec.WriteTo(buf)
c.Check(n, Equals, n2)
c.Check(buf.String(), Equals, f.b.String())
n, err := f.s.WriteTo(buf)
c.Assert(err, IsNil)
c.Check(n, Equals, int64(f.b.Len()))
c.Check(buf.String(), Equals, f.b.String())
}

func (f *S) TestReadFile(c *C) {
s := new(Spec)
err := s.ReadFile("../test_nut1/nut.json")
c.Check(err, IsNil)
c.Check(s, DeepEquals, f.s)
}

0 comments on commit 7e587e6

Please sign in to comment.