launchpad.net/~rogpeppe/juju-core/500-errgo-fix@v0.0.0-20140213181702-000000002356/cmd/filevar_test.go (about) 1 // Copyright 2012, 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package cmd_test 5 6 import ( 7 "os" 8 9 "launchpad.net/gnuflag" 10 gc "launchpad.net/gocheck" 11 12 "launchpad.net/juju-core/cmd" 13 "launchpad.net/juju-core/testing" 14 ) 15 16 type FileVarSuite struct { 17 ctx *cmd.Context 18 ValidPath string 19 InvalidPath string // invalid path refers to a file which is not readable 20 } 21 22 var _ = gc.Suite(&FileVarSuite{}) 23 24 func (s *FileVarSuite) SetUpTest(c *gc.C) { 25 s.ctx = testing.Context(c) 26 s.ValidPath = s.ctx.AbsPath("valid.yaml") 27 s.InvalidPath = s.ctx.AbsPath("invalid.yaml") 28 f, err := os.Create(s.ValidPath) 29 c.Assert(err, gc.IsNil) 30 f.Close() 31 f, err = os.Create(s.InvalidPath) 32 c.Assert(err, gc.IsNil) 33 f.Close() 34 err = os.Chmod(s.InvalidPath, 0) // make unreadable 35 c.Assert(err, gc.IsNil) 36 } 37 38 func (s *FileVarSuite) TestValidFileVar(c *gc.C) { 39 fs, config := fs() 40 err := fs.Parse(false, []string{"--config", s.ValidPath}) 41 c.Assert(err, gc.IsNil) 42 c.Assert(config.Path, gc.Equals, s.ValidPath) 43 _, err = config.Read(s.ctx) 44 c.Assert(err, gc.IsNil) 45 } 46 47 func (s *FileVarSuite) TestInvalidFileVar(c *gc.C) { 48 fs, config := fs() 49 err := fs.Parse(false, []string{"--config", s.InvalidPath}) 50 c.Assert(config.Path, gc.Equals, s.InvalidPath) 51 _, err = config.Read(s.ctx) 52 c.Assert(err, gc.ErrorMatches, "*permission denied") 53 } 54 55 func fs() (*gnuflag.FlagSet, *cmd.FileVar) { 56 var config cmd.FileVar 57 fs := testing.NewFlagSet() 58 fs.Var(&config, "config", "the config") 59 return fs, &config 60 }