github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/cmd/snap-failure/main_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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  	failure "github.com/snapcore/snapd/cmd/snap-failure"
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/logger"
    31  	"github.com/snapcore/snapd/testutil"
    32  )
    33  
    34  // Hook up check.v1 into the "go test" runner
    35  func Test(t *testing.T) { TestingT(t) }
    36  
    37  type failureSuite struct {
    38  	testutil.BaseTest
    39  
    40  	rootdir string
    41  
    42  	stderr *bytes.Buffer
    43  	stdout *bytes.Buffer
    44  	log    *bytes.Buffer
    45  }
    46  
    47  func (r *failureSuite) SetUpTest(c *C) {
    48  	r.stderr = bytes.NewBuffer(nil)
    49  	r.stdout = bytes.NewBuffer(nil)
    50  
    51  	oldStderr := failure.Stderr
    52  	oldStdout := failure.Stdout
    53  	r.AddCleanup(func() {
    54  		failure.Stderr = oldStderr
    55  		failure.Stdout = oldStdout
    56  	})
    57  	failure.Stderr = r.stderr
    58  	failure.Stdout = r.stdout
    59  
    60  	r.rootdir = c.MkDir()
    61  	dirs.SetRootDir(r.rootdir)
    62  	r.AddCleanup(func() { dirs.SetRootDir("/") })
    63  
    64  	log, restore := logger.MockLogger()
    65  	r.log = log
    66  	r.AddCleanup(restore)
    67  }
    68  
    69  func (r *failureSuite) Stderr() string {
    70  	return r.stderr.String()
    71  }
    72  
    73  func (r *failureSuite) Stdout() string {
    74  	return r.stdout.String()
    75  }
    76  
    77  var _ = Suite(&failureSuite{})
    78  
    79  func (r *failureSuite) TestUnknownArg(c *C) {
    80  	err := failure.ParseArgs([]string{})
    81  	c.Check(err, ErrorMatches, "Please specify the snapd command")
    82  }