Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Spec.ReadFile()

  • Loading branch information...
commit 7e587e6d4cdfb0c9591beebb001f76b489eec843 1 parent 19f70c7
@AlekSi authored
Showing with 47 additions and 22 deletions.
  1. +1 −0  CHANGELOG
  2. +20 −1 spec.go
  3. +26 −21 spec_test.go
View
1  CHANGELOG
@@ -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.
View
21 spec.go
@@ -6,6 +6,7 @@ import (
"io"
"io/ioutil"
"net/url"
+ "os"
"regexp"
"strings"
)
@@ -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.
@@ -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" {
View
47 spec_test.go
@@ -10,7 +10,7 @@ import (
)
type S struct {
- f *os.File
+ s *Spec
b *bytes.Buffer
}
@@ -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)
}
Please sign in to comment.
Something went wrong with that request. Please try again.