go.fuchsia.dev/jiri@v0.0.0-20240502161911-b66513b29486/cmdline/env_test.go (about)

     1  // Copyright 2015 The Vanadium Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  package cmdline
     5  
     6  import (
     7  	"bytes"
     8  	"io"
     9  	"os"
    10  	"testing"
    11  )
    12  
    13  func writeFunc(s string) func(*Env, io.Writer) {
    14  	return func(_ *Env, w io.Writer) { w.Write([]byte(s)) }
    15  }
    16  
    17  func TestEnvUsageErrorf(t *testing.T) {
    18  	tests := []struct {
    19  		format string
    20  		args   []interface{}
    21  		usage  func(*Env, io.Writer)
    22  		want   string
    23  	}{
    24  		{"", nil, nil, "ERROR: \n\nusage error\n"},
    25  		{"", nil, writeFunc("FooBar"), "ERROR: \n\nFooBar"},
    26  		{"", nil, writeFunc("FooBar\n"), "ERROR: \n\nFooBar\n"},
    27  		{"A%vB", []interface{}{"x"}, nil, "ERROR: AxB\n\nusage error\n"},
    28  		{"A%vB", []interface{}{"x"}, writeFunc("FooBar"), "ERROR: AxB\n\nFooBar"},
    29  		{"A%vB", []interface{}{"x"}, writeFunc("FooBar\n"), "ERROR: AxB\n\nFooBar\n"},
    30  	}
    31  	for _, test := range tests {
    32  		var buf bytes.Buffer
    33  		env := &Env{Stderr: &buf, Usage: test.usage}
    34  		if got, want := env.UsageErrorf(test.format, test.args...), ErrUsage; got != want {
    35  			t.Errorf("%q got error %v, want %v", test.want, got, want)
    36  		}
    37  		if got, want := buf.String(), test.want; got != want {
    38  			t.Errorf("got %v, want %v", got, want)
    39  		}
    40  	}
    41  }
    42  
    43  func TestEnvWidth(t *testing.T) {
    44  	tests := []struct {
    45  		value string
    46  		want  int
    47  	}{
    48  		{"123", 123},
    49  		{"-1", -1},
    50  		{"0", defaultWidth},
    51  		{"", defaultWidth},
    52  		{"foobar", defaultWidth},
    53  	}
    54  	for _, test := range tests {
    55  		// Test using a fake environment.
    56  		env := &Env{Vars: map[string]string{"CMDLINE_WIDTH": test.value}}
    57  		if got, want := env.width(), test.want; got != want {
    58  			t.Errorf("%q got %v, want %v", test.value, got, want)
    59  		}
    60  		// Test using the OS environment.
    61  		if err := os.Setenv("CMDLINE_WIDTH", test.value); err != nil {
    62  			t.Errorf("Setenv(%q) failed: %v", test.value, err)
    63  		} else if got, want := EnvFromOS().width(), test.want; got != want {
    64  			t.Errorf("%q got %v, want %v", test.value, got, want)
    65  		}
    66  	}
    67  	os.Unsetenv("CMDLINE_WIDTH")
    68  }
    69  
    70  func TestEnvStyle(t *testing.T) {
    71  	tests := []struct {
    72  		value string
    73  		want  style
    74  	}{
    75  		{"compact", styleCompact},
    76  		{"full", styleFull},
    77  		{"godoc", styleGoDoc},
    78  		{"", styleCompact},
    79  		{"abc", styleCompact},
    80  		{"foobar", styleCompact},
    81  	}
    82  	for _, test := range tests {
    83  		// Test using a fake environment.
    84  		env := &Env{Vars: map[string]string{"CMDLINE_STYLE": test.value}}
    85  		if got, want := env.style(), test.want; got != want {
    86  			t.Errorf("%q got %v, want %v", test.value, got, want)
    87  		}
    88  		// Test using the OS environment.
    89  		if err := os.Setenv("CMDLINE_STYLE", test.value); err != nil {
    90  			t.Errorf("Setenv(%q) failed: %v", test.value, err)
    91  		} else if got, want := EnvFromOS().style(), test.want; got != want {
    92  			t.Errorf("%q got %v, want %v", test.value, got, want)
    93  		}
    94  	}
    95  	os.Unsetenv("CMDLINE_STYLE")
    96  }