github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/cmd/snap-repair/main_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2017-2020 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 main_test
    21  
    22  import (
    23  	"bytes"
    24  	"testing"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	repair "github.com/snapcore/snapd/cmd/snap-repair"
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/release"
    31  	"github.com/snapcore/snapd/snapdenv"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  // Hook up check.v1 into the "go test" runner
    36  func Test(t *testing.T) { TestingT(t) }
    37  
    38  type repairSuite struct {
    39  	testutil.BaseTest
    40  	baseRunnerSuite
    41  
    42  	rootdir string
    43  
    44  	stdout *bytes.Buffer
    45  	stderr *bytes.Buffer
    46  
    47  	restore func()
    48  }
    49  
    50  func (r *repairSuite) SetUpSuite(c *C) {
    51  	r.baseRunnerSuite.SetUpSuite(c)
    52  	r.restore = snapdenv.SetUserAgentFromVersion("", nil, "")
    53  }
    54  
    55  func (r *repairSuite) TearDownSuite(c *C) {
    56  	r.restore()
    57  }
    58  
    59  func (r *repairSuite) SetUpTest(c *C) {
    60  	r.BaseTest.SetUpTest(c)
    61  	r.baseRunnerSuite.SetUpTest(c)
    62  
    63  	r.stdout = bytes.NewBuffer(nil)
    64  	r.stderr = bytes.NewBuffer(nil)
    65  
    66  	oldStdout := repair.Stdout
    67  	r.AddCleanup(func() { repair.Stdout = oldStdout })
    68  	repair.Stdout = r.stdout
    69  
    70  	oldStderr := repair.Stderr
    71  	r.AddCleanup(func() { repair.Stderr = oldStderr })
    72  	repair.Stderr = r.stderr
    73  
    74  	r.rootdir = c.MkDir()
    75  	dirs.SetRootDir(r.rootdir)
    76  	r.AddCleanup(func() { dirs.SetRootDir("/") })
    77  }
    78  
    79  func (r *repairSuite) TearDownTest(c *C) {
    80  	r.baseRunnerSuite.TearDownTest(c)
    81  	r.BaseTest.TearDownTest(c)
    82  }
    83  
    84  func (r *repairSuite) Stdout() string {
    85  	return r.stdout.String()
    86  }
    87  
    88  func (r *repairSuite) Stderr() string {
    89  	return r.stderr.String()
    90  }
    91  
    92  var _ = Suite(&repairSuite{})
    93  
    94  func (r *repairSuite) TestUnknownArg(c *C) {
    95  	err := repair.ParseArgs([]string{})
    96  	c.Check(err, ErrorMatches, "Please specify one command of: list, run or show")
    97  }
    98  
    99  func (r *repairSuite) TestRunOnClassic(c *C) {
   100  	defer release.MockOnClassic(true)()
   101  
   102  	err := repair.Run()
   103  	c.Check(err, ErrorMatches, "cannot use snap-repair on a classic system")
   104  }