github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/color_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  	"os"
    24  	"runtime"
    25  
    26  	"gopkg.in/check.v1"
    27  
    28  	cmdsnap "github.com/snapcore/snapd/cmd/snap"
    29  	"github.com/snapcore/snapd/snap"
    30  )
    31  
    32  func setEnviron(env map[string]string) func() {
    33  	old := make(map[string]string, len(env))
    34  	ok := make(map[string]bool, len(env))
    35  
    36  	for k, v := range env {
    37  		old[k], ok[k] = os.LookupEnv(k)
    38  		if v != "" {
    39  			os.Setenv(k, v)
    40  		} else {
    41  			os.Unsetenv(k)
    42  		}
    43  	}
    44  
    45  	return func() {
    46  		for k := range ok {
    47  			if ok[k] {
    48  				os.Setenv(k, old[k])
    49  			} else {
    50  				os.Unsetenv(k)
    51  			}
    52  		}
    53  	}
    54  }
    55  
    56  func (s *SnapSuite) TestCanUnicode(c *check.C) {
    57  	// setenv is per thread
    58  	runtime.LockOSThread()
    59  	defer runtime.UnlockOSThread()
    60  
    61  	type T struct {
    62  		lang, lcAll, lcMsg string
    63  		expected           bool
    64  	}
    65  
    66  	for _, t := range []T{
    67  		{expected: false}, // all locale unset
    68  		{lang: "C", expected: false},
    69  		{lang: "C", lcAll: "C", expected: false},
    70  		{lang: "C", lcAll: "C", lcMsg: "C", expected: false},
    71  		{lang: "C.UTF-8", lcAll: "C", lcMsg: "C", expected: false}, // LC_MESSAGES wins
    72  		{lang: "C.UTF-8", lcAll: "C.UTF-8", lcMsg: "C", expected: false},
    73  		{lang: "C.UTF-8", lcAll: "C.UTF-8", lcMsg: "C.UTF-8", expected: true},
    74  		{lang: "C.UTF-8", lcAll: "C", lcMsg: "C.UTF-8", expected: true},
    75  		{lang: "C", lcAll: "C", lcMsg: "C.UTF-8", expected: true},
    76  		{lang: "C", lcAll: "C.UTF-8", expected: true},
    77  		{lang: "C.UTF-8", expected: true},
    78  		{lang: "C.utf8", expected: true}, // deals with a bit of rando weirdness
    79  	} {
    80  		restore := setEnviron(map[string]string{"LANG": t.lang, "LC_ALL": t.lcAll, "LC_MESSAGES": t.lcMsg})
    81  		c.Check(cmdsnap.CanUnicode("never"), check.Equals, false)
    82  		c.Check(cmdsnap.CanUnicode("always"), check.Equals, true)
    83  		restoreIsTTY := cmdsnap.MockIsStdoutTTY(true)
    84  		c.Check(cmdsnap.CanUnicode("auto"), check.Equals, t.expected)
    85  		cmdsnap.MockIsStdoutTTY(false)
    86  		c.Check(cmdsnap.CanUnicode("auto"), check.Equals, false)
    87  		restoreIsTTY()
    88  		restore()
    89  	}
    90  }
    91  
    92  func (s *SnapSuite) TestColorTable(c *check.C) {
    93  	// setenv is per thread
    94  	runtime.LockOSThread()
    95  	defer runtime.UnlockOSThread()
    96  
    97  	type T struct {
    98  		isTTY         bool
    99  		noColor, term string
   100  		expected      interface{}
   101  		desc          string
   102  	}
   103  
   104  	for _, t := range []T{
   105  		{isTTY: false, expected: cmdsnap.NoEscColorTable, desc: "not a tty"},
   106  		{isTTY: false, noColor: "1", expected: cmdsnap.NoEscColorTable, desc: "no tty *and* NO_COLOR set"},
   107  		{isTTY: false, term: "linux-m", expected: cmdsnap.NoEscColorTable, desc: "no tty *and* mono term set"},
   108  		{isTTY: true, expected: cmdsnap.ColorColorTable, desc: "is a tty"},
   109  		{isTTY: true, noColor: "1", expected: cmdsnap.MonoColorTable, desc: "is a tty, but NO_COLOR set"},
   110  		{isTTY: true, term: "linux-m", expected: cmdsnap.MonoColorTable, desc: "is a tty, but TERM=linux-m"},
   111  		{isTTY: true, term: "xterm-mono", expected: cmdsnap.MonoColorTable, desc: "is a tty, but TERM=xterm-mono"},
   112  	} {
   113  		restoreIsTTY := cmdsnap.MockIsStdoutTTY(t.isTTY)
   114  		restoreEnv := setEnviron(map[string]string{"NO_COLOR": t.noColor, "TERM": t.term})
   115  		c.Check(cmdsnap.ColorTable("never"), check.DeepEquals, cmdsnap.NoEscColorTable, check.Commentf(t.desc))
   116  		c.Check(cmdsnap.ColorTable("always"), check.DeepEquals, cmdsnap.ColorColorTable, check.Commentf(t.desc))
   117  		c.Check(cmdsnap.ColorTable("auto"), check.DeepEquals, t.expected, check.Commentf(t.desc))
   118  		restoreEnv()
   119  		restoreIsTTY()
   120  	}
   121  }
   122  
   123  func (s *SnapSuite) TestPublisherEscapes(c *check.C) {
   124  	// just check never/always; for auto checks look above
   125  	type T struct {
   126  		color, unicode    bool
   127  		username, display string
   128  		verified          bool
   129  		short, long, fill string
   130  	}
   131  	for _, t := range []T{
   132  		// non-verified equal under fold:
   133  		{color: false, unicode: false, username: "potato", display: "Potato",
   134  			short: "potato", long: "Potato", fill: ""},
   135  		{color: false, unicode: true, username: "potato", display: "Potato",
   136  			short: "potato", long: "Potato", fill: ""},
   137  		{color: true, unicode: false, username: "potato", display: "Potato",
   138  			short: "potato\x1b[32m\x1b[0m", long: "Potato\x1b[32m\x1b[0m", fill: "\x1b[32m\x1b[0m"},
   139  		{color: true, unicode: true, username: "potato", display: "Potato",
   140  			short: "potato\x1b[32m\x1b[0m", long: "Potato\x1b[32m\x1b[0m", fill: "\x1b[32m\x1b[0m"},
   141  		// verified equal under fold:
   142  		{color: false, unicode: false, username: "potato", display: "Potato", verified: true,
   143  			short: "potato*", long: "Potato*", fill: ""},
   144  		{color: false, unicode: true, username: "potato", display: "Potato", verified: true,
   145  			short: "potato✓", long: "Potato✓", fill: ""},
   146  		{color: true, unicode: false, username: "potato", display: "Potato", verified: true,
   147  			short: "potato\x1b[32m*\x1b[0m", long: "Potato\x1b[32m*\x1b[0m", fill: "\x1b[32m\x1b[0m"},
   148  		{color: true, unicode: true, username: "potato", display: "Potato", verified: true,
   149  			short: "potato\x1b[32m✓\x1b[0m", long: "Potato\x1b[32m✓\x1b[0m", fill: "\x1b[32m\x1b[0m"},
   150  		// non-verified, different
   151  		{color: false, unicode: false, username: "potato", display: "Carrot",
   152  			short: "potato", long: "Carrot (potato)", fill: ""},
   153  		{color: false, unicode: true, username: "potato", display: "Carrot",
   154  			short: "potato", long: "Carrot (potato)", fill: ""},
   155  		{color: true, unicode: false, username: "potato", display: "Carrot",
   156  			short: "potato\x1b[32m\x1b[0m", long: "Carrot (potato\x1b[32m\x1b[0m)", fill: "\x1b[32m\x1b[0m"},
   157  		{color: true, unicode: true, username: "potato", display: "Carrot",
   158  			short: "potato\x1b[32m\x1b[0m", long: "Carrot (potato\x1b[32m\x1b[0m)", fill: "\x1b[32m\x1b[0m"},
   159  		// verified, different
   160  		{color: false, unicode: false, username: "potato", display: "Carrot", verified: true,
   161  			short: "potato*", long: "Carrot (potato*)", fill: ""},
   162  		{color: false, unicode: true, username: "potato", display: "Carrot", verified: true,
   163  			short: "potato✓", long: "Carrot (potato✓)", fill: ""},
   164  		{color: true, unicode: false, username: "potato", display: "Carrot", verified: true,
   165  			short: "potato\x1b[32m*\x1b[0m", long: "Carrot (potato\x1b[32m*\x1b[0m)", fill: "\x1b[32m\x1b[0m"},
   166  		{color: true, unicode: true, username: "potato", display: "Carrot", verified: true,
   167  			short: "potato\x1b[32m✓\x1b[0m", long: "Carrot (potato\x1b[32m✓\x1b[0m)", fill: "\x1b[32m\x1b[0m"},
   168  		// some interesting equal-under-folds:
   169  		{color: false, unicode: false, username: "potato", display: "PoTaTo",
   170  			short: "potato", long: "PoTaTo", fill: ""},
   171  		{color: false, unicode: false, username: "potato-team", display: "Potato Team",
   172  			short: "potato-team", long: "Potato Team", fill: ""},
   173  	} {
   174  		pub := &snap.StoreAccount{Username: t.username, DisplayName: t.display}
   175  		if t.verified {
   176  			pub.Validation = "verified"
   177  		}
   178  		color := "never"
   179  		if t.color {
   180  			color = "always"
   181  		}
   182  		unicode := "never"
   183  		if t.unicode {
   184  			unicode = "always"
   185  		}
   186  
   187  		mx := cmdsnap.ColorMixin(color, unicode)
   188  		esc := cmdsnap.ColorMixinGetEscapes(mx)
   189  
   190  		c.Check(cmdsnap.ShortPublisher(esc, pub), check.Equals, t.short)
   191  		c.Check(cmdsnap.LongPublisher(esc, pub), check.Equals, t.long)
   192  		c.Check(cmdsnap.FillerPublisher(esc), check.Equals, t.fill)
   193  	}
   194  }