github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_alias_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 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 main_test
    21  
    22  import (
    23  	"fmt"
    24  	"net/http"
    25  
    26  	. "gopkg.in/check.v1"
    27  
    28  	. "github.com/snapcore/snapd/cmd/snap"
    29  )
    30  
    31  func (s *SnapSuite) TestAliasHelp(c *C) {
    32  	msg := `Usage:
    33    snap.test alias [alias-OPTIONS] [<snap.app>] [<alias>]
    34  
    35  The alias command aliases the given snap application to the given alias.
    36  
    37  Once this manual alias is setup the respective application command can be
    38  invoked just using the alias.
    39  
    40  [alias command options]
    41        --no-wait       Do not wait for the operation to finish but just print
    42                        the change id.
    43  `
    44  	s.testSubCommandHelp(c, "alias", msg)
    45  }
    46  
    47  func (s *SnapSuite) TestAlias(c *C) {
    48  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    49  		switch r.URL.Path {
    50  		case "/v2/aliases":
    51  			c.Check(r.Method, Equals, "POST")
    52  			c.Check(DecodedRequestBody(c, r), DeepEquals, map[string]interface{}{
    53  				"action": "alias",
    54  				"snap":   "alias-snap",
    55  				"app":    "cmd1",
    56  				"alias":  "alias1",
    57  			})
    58  			w.WriteHeader(202)
    59  			fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`)
    60  		case "/v2/changes/zzz":
    61  			c.Check(r.Method, Equals, "GET")
    62  			fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done", "data": {"aliases-added": [{"alias": "alias1", "snap": "alias-snap", "app": "cmd1"}]}}}`)
    63  		default:
    64  			c.Fatalf("unexpected path %q", r.URL.Path)
    65  		}
    66  	})
    67  	rest, err := Parser(Client()).ParseArgs([]string{"alias", "alias-snap.cmd1", "alias1"})
    68  	c.Assert(err, IsNil)
    69  	c.Assert(rest, DeepEquals, []string{})
    70  	c.Assert(s.Stdout(), Equals, ""+
    71  		"Added:\n"+
    72  		"  - alias-snap.cmd1 as alias1\n",
    73  	)
    74  	c.Assert(s.Stderr(), Equals, "")
    75  }