github.com/hugh712/snapd@v0.0.0-20200910133618-1a99902bd583/snap/revision_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2016 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 snap_test
    21  
    22  import (
    23  	"encoding/json"
    24  	"strconv"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	"gopkg.in/yaml.v2"
    29  
    30  	"github.com/snapcore/snapd/snap"
    31  )
    32  
    33  // Keep this in sync between snap and client packages.
    34  
    35  type revisionSuite struct{}
    36  
    37  var _ = Suite(&revisionSuite{})
    38  
    39  func (s revisionSuite) TestString(c *C) {
    40  	c.Assert(snap.R(0).String(), Equals, "unset")
    41  	c.Assert(snap.R(10).String(), Equals, "10")
    42  	c.Assert(snap.R(-9).String(), Equals, "x9")
    43  }
    44  
    45  func (s revisionSuite) TestUnset(c *C) {
    46  	c.Assert(snap.R(0).Unset(), Equals, true)
    47  	c.Assert(snap.R(10).Unset(), Equals, false)
    48  	c.Assert(snap.R(-9).Unset(), Equals, false)
    49  }
    50  
    51  func (s revisionSuite) TestLocal(c *C) {
    52  	c.Assert(snap.R(0).Local(), Equals, false)
    53  	c.Assert(snap.R(10).Local(), Equals, false)
    54  	c.Assert(snap.R(-9).Local(), Equals, true)
    55  }
    56  
    57  func (s revisionSuite) TestStore(c *C) {
    58  	c.Assert(snap.R(0).Store(), Equals, false)
    59  	c.Assert(snap.R(10).Store(), Equals, true)
    60  	c.Assert(snap.R(-9).Store(), Equals, false)
    61  }
    62  
    63  func (s revisionSuite) TestJSON(c *C) {
    64  	for _, n := range []int{0, 10, -9} {
    65  		r := snap.R(n)
    66  		data, err := json.Marshal(snap.R(n))
    67  		c.Assert(err, IsNil)
    68  		c.Assert(string(data), Equals, `"`+r.String()+`"`)
    69  
    70  		var got snap.Revision
    71  		err = json.Unmarshal(data, &got)
    72  		c.Assert(err, IsNil)
    73  		c.Assert(got, Equals, r)
    74  
    75  		got = snap.Revision{}
    76  		err = json.Unmarshal([]byte(strconv.Itoa(r.N)), &got)
    77  		c.Assert(err, IsNil)
    78  		c.Assert(got, Equals, r)
    79  	}
    80  }
    81  
    82  func (s revisionSuite) TestYAML(c *C) {
    83  	for _, v := range []struct {
    84  		n int
    85  		s string
    86  	}{
    87  		{0, "unset"},
    88  		{10, `"10"`},
    89  		{-9, "x9"},
    90  	} {
    91  		r := snap.R(v.n)
    92  		data, err := yaml.Marshal(snap.R(v.n))
    93  		c.Assert(err, IsNil)
    94  		c.Assert(string(data), Equals, v.s+"\n")
    95  
    96  		var got snap.Revision
    97  		err = yaml.Unmarshal(data, &got)
    98  		c.Assert(err, IsNil)
    99  		c.Assert(got, Equals, r)
   100  
   101  		got = snap.Revision{}
   102  		err = json.Unmarshal([]byte(strconv.Itoa(r.N)), &got)
   103  		c.Assert(err, IsNil)
   104  		c.Assert(got, Equals, r)
   105  	}
   106  }
   107  
   108  func (s revisionSuite) ParseRevision(c *C) {
   109  	type testItem struct {
   110  		s string
   111  		n int
   112  		e string
   113  	}
   114  
   115  	var tests = []testItem{{
   116  		s: "unset",
   117  		n: 0,
   118  	}, {
   119  		s: "x1",
   120  		n: -1,
   121  	}, {
   122  		s: "1",
   123  		n: 1,
   124  	}, {
   125  		s: "x-1",
   126  		e: `invalid snap revision: "x-1"`,
   127  	}, {
   128  		s: "x0",
   129  		e: `invalid snap revision: "x0"`,
   130  	}, {
   131  		s: "-1",
   132  		e: `invalid snap revision: "-1"`,
   133  	}, {
   134  		s: "0",
   135  		e: `invalid snap revision: "0"`,
   136  	}}
   137  
   138  	for _, test := range tests {
   139  		r, err := snap.ParseRevision(test.s)
   140  		if test.e != "" {
   141  			c.Assert(err.Error(), Equals, test.e)
   142  			continue
   143  		}
   144  		c.Assert(r, Equals, snap.R(test.n))
   145  	}
   146  }
   147  
   148  func (s *revisionSuite) TestR(c *C) {
   149  	type testItem struct {
   150  		v interface{}
   151  		n int
   152  		e string
   153  	}
   154  
   155  	var tests = []testItem{{
   156  		v: 0,
   157  		n: 0,
   158  	}, {
   159  		v: -1,
   160  		n: -1,
   161  	}, {
   162  		v: 1,
   163  		n: 1,
   164  	}, {
   165  		v: "unset",
   166  		n: 0,
   167  	}, {
   168  		v: "x1",
   169  		n: -1,
   170  	}, {
   171  		v: "1",
   172  		n: 1,
   173  	}, {
   174  		v: "x-1",
   175  		e: `invalid snap revision: "x-1"`,
   176  	}, {
   177  		v: "x0",
   178  		e: `invalid snap revision: "x0"`,
   179  	}, {
   180  		v: "-1",
   181  		e: `invalid snap revision: "-1"`,
   182  	}, {
   183  		v: "0",
   184  		e: `invalid snap revision: "0"`,
   185  	}, {
   186  		v: int64(1),
   187  		e: `cannot use 1 \(int64\) as a snap revision`,
   188  	}}
   189  
   190  	for _, test := range tests {
   191  		if test.e != "" {
   192  			f := func() { snap.R(test.v) }
   193  			c.Assert(f, PanicMatches, test.e)
   194  			continue
   195  		}
   196  
   197  		c.Assert(snap.R(test.v), Equals, snap.R(test.n))
   198  	}
   199  }