github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/advisor/cmdfinder_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 advisor_test
    21  
    22  import (
    23  	"os"
    24  	"sort"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/advisor"
    30  	"github.com/snapcore/snapd/dirs"
    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 cmdfinderSuite struct{}
    38  
    39  var _ = Suite(&cmdfinderSuite{})
    40  
    41  func (s *cmdfinderSuite) SetUpTest(c *C) {
    42  	dirs.SetRootDir(c.MkDir())
    43  	c.Assert(os.MkdirAll(dirs.SnapCacheDir, 0755), IsNil)
    44  
    45  	db, err := advisor.Create()
    46  	c.Assert(err, IsNil)
    47  	c.Assert(db.AddSnap("foo", "1.0", "foo summary", []string{"foo", "meh"}), IsNil)
    48  	c.Assert(db.AddSnap("bar", "2.0", "bar summary", []string{"bar", "meh"}), IsNil)
    49  	c.Assert(db.Commit(), IsNil)
    50  }
    51  
    52  func (s *cmdfinderSuite) TestFindSimilarWordsCnf(c *C) {
    53  	words := advisor.SimilarWords("123")
    54  	sort.Strings(words)
    55  	c.Check(words, DeepEquals, []string{
    56  		// calculated using CommandNotFound.py:similar_words("123")
    57  		"-123", "-23", "0123", "023", "1-23", "1-3", "1023",
    58  		"103", "1123", "113", "12", "12-", "12-3", "120",
    59  		"1203", "121", "1213", "122", "1223", "123", "1233",
    60  		"124", "1243", "125", "1253", "126", "1263", "127",
    61  		"1273", "128", "1283", "129", "1293", "12_", "12_3",
    62  		"12a", "12a3", "12b", "12b3", "12c", "12c3", "12d",
    63  		"12d3", "12e", "12e3", "12f", "12f3", "12g", "12g3",
    64  		"12h", "12h3", "12i", "12i3", "12j", "12j3", "12k",
    65  		"12k3", "12l", "12l3", "12m", "12m3", "12n", "12n3",
    66  		"12o", "12o3", "12p", "12p3", "12q", "12q3", "12r",
    67  		"12r3", "12s", "12s3", "12t", "12t3", "12u", "12u3",
    68  		"12v", "12v3", "12w", "12w3", "12x", "12x3", "12y",
    69  		"12y3", "12z", "12z3", "13", "132", "1323", "133",
    70  		"1423", "143", "1523", "153", "1623", "163", "1723",
    71  		"173", "1823", "183", "1923", "193", "1_23", "1_3",
    72  		"1a23", "1a3", "1b23", "1b3", "1c23", "1c3", "1d23",
    73  		"1d3", "1e23", "1e3", "1f23", "1f3", "1g23", "1g3",
    74  		"1h23", "1h3", "1i23", "1i3", "1j23", "1j3", "1k23",
    75  		"1k3", "1l23", "1l3", "1m23", "1m3", "1n23", "1n3",
    76  		"1o23", "1o3", "1p23", "1p3", "1q23", "1q3", "1r23",
    77  		"1r3", "1s23", "1s3", "1t23", "1t3", "1u23", "1u3",
    78  		"1v23", "1v3", "1w23", "1w3", "1x23", "1x3", "1y23",
    79  		"1y3", "1z23", "1z3", "2123", "213", "223", "23",
    80  		"3123", "323", "4123", "423", "5123", "523", "6123",
    81  		"623", "7123", "723", "8123", "823", "9123", "923",
    82  		"_123", "_23", "a123", "a23", "b123", "b23", "c123",
    83  		"c23", "d123", "d23", "e123", "e23", "f123", "f23",
    84  		"g123", "g23", "h123", "h23", "i123", "i23", "j123",
    85  		"j23", "k123", "k23", "l123", "l23", "m123", "m23",
    86  		"n123", "n23", "o123", "o23", "p123", "p23", "q123",
    87  		"q23", "r123", "r23", "s123", "s23", "t123", "t23",
    88  		"u123", "u23", "v123", "v23", "w123", "w23", "x123",
    89  		"x23", "y123", "y23", "z123", "z23",
    90  	})
    91  }
    92  
    93  func (s *cmdfinderSuite) TestFindSimilarWordsTrivial(c *C) {
    94  	words := advisor.SimilarWords("hella")
    95  	c.Check(words, testutil.Contains, "hello")
    96  }
    97  
    98  func (s *cmdfinderSuite) TestFindCommandHit(c *C) {
    99  	cmds, err := advisor.FindCommand("meh")
   100  	c.Assert(err, IsNil)
   101  	c.Check(cmds, DeepEquals, []advisor.Command{
   102  		{Snap: "foo", Version: "1.0", Command: "meh"},
   103  		{Snap: "bar", Version: "2.0", Command: "meh"},
   104  	})
   105  }
   106  
   107  func (s *cmdfinderSuite) TestFindCommandMiss(c *C) {
   108  	cmds, err := advisor.FindCommand("moh")
   109  	c.Assert(err, IsNil)
   110  	c.Check(cmds, HasLen, 0)
   111  }
   112  
   113  func (s *cmdfinderSuite) TestFindMisspelledCommandHit(c *C) {
   114  	cmds, err := advisor.FindMisspelledCommand("moh")
   115  	c.Assert(err, IsNil)
   116  	c.Check(cmds, DeepEquals, []advisor.Command{
   117  		{Snap: "foo", Version: "1.0", Command: "meh"},
   118  		{Snap: "bar", Version: "2.0", Command: "meh"},
   119  	})
   120  }
   121  
   122  func (s *cmdfinderSuite) TestFindMisspelledCommandMiss(c *C) {
   123  	cmds, err := advisor.FindMisspelledCommand("hello")
   124  	c.Assert(err, IsNil)
   125  	c.Check(cmds, HasLen, 0)
   126  }
   127  
   128  func (s *cmdfinderSuite) TestDumpCommands(c *C) {
   129  	cmds, err := advisor.DumpCommands()
   130  	c.Assert(err, IsNil)
   131  	c.Check(cmds, DeepEquals, map[string]string{
   132  		"foo": `[{"snap":"foo","version":"1.0"}]`,
   133  		"bar": `[{"snap":"bar","version":"2.0"}]`,
   134  		"meh": `[{"snap":"foo","version":"1.0"},{"snap":"bar","version":"2.0"}]`,
   135  	})
   136  }
   137  
   138  func (s *cmdfinderSuite) TestFindMissingCommandsDB(c *C) {
   139  	err := os.Remove(dirs.SnapCommandsDB)
   140  	c.Assert(err, IsNil)
   141  
   142  	cmds, err := advisor.FindMisspelledCommand("hello")
   143  	c.Assert(err, IsNil)
   144  	c.Check(cmds, HasLen, 0)
   145  
   146  	cmds, err = advisor.FindCommand("hello")
   147  	c.Assert(err, IsNil)
   148  	c.Check(cmds, HasLen, 0)
   149  
   150  	pkg, err := advisor.FindPackage("hello")
   151  	c.Assert(err, IsNil)
   152  	c.Check(pkg, IsNil)
   153  }