github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/osutil/testhelper_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  
    25  	. "gopkg.in/check.v1"
    26  
    27  	"github.com/snapcore/snapd/osutil"
    28  )
    29  
    30  type testhelperSuite struct{}
    31  
    32  var _ = Suite(&testhelperSuite{})
    33  
    34  func mockOsArgs(args []string) (restore func()) {
    35  	old := os.Args
    36  	os.Args = args
    37  	return func() {
    38  		os.Args = old
    39  	}
    40  }
    41  
    42  func (s *testhelperSuite) TestIsTestBinary(c *C) {
    43  	// obvious case
    44  	c.Assert(osutil.IsTestBinary(), Equals, true)
    45  
    46  	defer mockOsArgs([]string{"foo", "bar", "baz"})()
    47  	c.Assert(osutil.IsTestBinary(), Equals, false)
    48  }
    49  
    50  func (s *testhelperSuite) TestMustBeTestBinary(c *C) {
    51  	// obvious case
    52  	osutil.MustBeTestBinary("unexpected panic")
    53  
    54  	defer mockOsArgs([]string{"foo", "bar", "baz"})()
    55  	c.Assert(func() { osutil.MustBeTestBinary("panic message") }, PanicMatches, "panic message")
    56  }
    57  
    58  func (s *testhelperSuite) TestBinaryNoRegressionWithValidApp(c *C) {
    59  	// a snap app named 'test' is valid, we must not be confused here
    60  	defer mockOsArgs([]string{"/snap/bin/some-snap.test", "bar", "baz"})()
    61  	// must not be considered a test binary
    62  	c.Assert(osutil.IsTestBinary(), Equals, false)
    63  	// must panic since binary is a non-test one
    64  	c.Assert(func() { osutil.MustBeTestBinary("non test binary, expecting a panic") },
    65  		PanicMatches, "non test binary, expecting a panic")
    66  }