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