github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/osutil/exitcode_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  	"os"
    24  	"os/exec"
    25  
    26  	. "gopkg.in/check.v1"
    27  )
    28  
    29  type ExitCodeTestSuite struct{}
    30  
    31  var _ = Suite(&ExitCodeTestSuite{})
    32  
    33  func (ts *ExitCodeTestSuite) TestExitCode(c *C) {
    34  	cmd := exec.Command("true")
    35  	err := cmd.Run()
    36  	c.Assert(err, IsNil)
    37  
    38  	cmd = exec.Command("false")
    39  	err = cmd.Run()
    40  	c.Assert(err, NotNil)
    41  	e, err := ExitCode(err)
    42  	c.Assert(err, IsNil)
    43  	c.Assert(e, Equals, 1)
    44  
    45  	cmd = exec.Command("sh", "-c", "exit 7")
    46  	err = cmd.Run()
    47  	e, err = ExitCode(err)
    48  	c.Assert(err, IsNil)
    49  	c.Assert(e, Equals, 7)
    50  
    51  	// ensure that non exec.ExitError values give a error
    52  	_, err = os.Stat("/random/file/that/is/not/there")
    53  	c.Assert(err, NotNil)
    54  	_, err = ExitCode(err)
    55  	c.Assert(err, NotNil)
    56  }