github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/osutil/chdir_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 Canonical Ltd
     5   *
     6   * This program is free software: you can redistribute it and/or modify
     7   * it under the terms of the GNU General Public License version 3 as
     8   * published by the Free Software Foundation.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17   *
    18   */
    19  
    20  package osutil
    21  
    22  import (
    23  	"fmt"
    24  	"os"
    25  
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  type ChdirTestSuite struct{}
    30  
    31  var _ = Suite(&ChdirTestSuite{})
    32  
    33  func (ts *ChdirTestSuite) TestChdir(c *C) {
    34  	tmpdir := c.MkDir()
    35  
    36  	cwd, err := os.Getwd()
    37  	c.Assert(err, IsNil)
    38  	c.Assert(cwd, Not(Equals), tmpdir)
    39  	ChDir(tmpdir, func() error {
    40  		cwd, err := os.Getwd()
    41  		c.Assert(err, IsNil)
    42  		c.Assert(cwd, Equals, tmpdir)
    43  		return err
    44  	})
    45  }
    46  
    47  func (ts *ChdirTestSuite) TestChdirErrorNoDir(c *C) {
    48  	err := ChDir("random-dir-that-does-not-exist", func() error {
    49  		return nil
    50  	})
    51  	c.Assert(err, ErrorMatches, "chdir .*: no such file or directory")
    52  }
    53  
    54  func (ts *ChdirTestSuite) TestChdirErrorFromFunc(c *C) {
    55  	err := ChDir("/", func() error {
    56  		return fmt.Errorf("meep")
    57  	})
    58  	c.Assert(err, ErrorMatches, "meep")
    59  }