github.com/freetocompute/snapd@v0.0.0-20210618182524-2fb355d72fd9/testutil/filepresencechecker_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2015-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 testutil_test
    21  
    22  import (
    23  	"fmt"
    24  	"io/ioutil"
    25  	"path/filepath"
    26  
    27  	"gopkg.in/check.v1"
    28  
    29  	. "github.com/snapcore/snapd/testutil"
    30  )
    31  
    32  type filePresenceCheckerSuite struct{}
    33  
    34  var _ = check.Suite(&filePresenceCheckerSuite{})
    35  
    36  func (*filePresenceCheckerSuite) TestFilePresent(c *check.C) {
    37  	d := c.MkDir()
    38  	filename := filepath.Join(d, "foo")
    39  	testInfo(c, FilePresent, "FilePresent", []string{"filename"})
    40  	testCheck(c, FilePresent, false, `filename must be a string`, 42)
    41  	testCheck(c, FilePresent, false, fmt.Sprintf(`file %q is absent but should exist`, filename), filename)
    42  	c.Assert(ioutil.WriteFile(filename, nil, 0644), check.IsNil)
    43  	testCheck(c, FilePresent, true, "", filename)
    44  }
    45  
    46  func (*filePresenceCheckerSuite) TestFileAbsent(c *check.C) {
    47  	d := c.MkDir()
    48  	filename := filepath.Join(d, "foo")
    49  	testInfo(c, FileAbsent, "FileAbsent", []string{"filename"})
    50  	testCheck(c, FileAbsent, false, `filename must be a string`, 42)
    51  	testCheck(c, FileAbsent, true, "", filename)
    52  	c.Assert(ioutil.WriteFile(filename, nil, 0644), check.IsNil)
    53  	testCheck(c, FileAbsent, false, fmt.Sprintf(`file %q is present but should not exist`, filename), filename)
    54  }