github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/pkg/cmd/common_test.go (about)

     1  package cmd
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func TestParseMemory(t *testing.T) {
     9  	testCases := []struct {
    10  		memory   string
    11  		expected uint64
    12  	}{
    13  		{
    14  			memory:   "5000000",
    15  			expected: 5000000,
    16  		},
    17  		{
    18  			memory:   "6MB",
    19  			expected: 6000000,
    20  		},
    21  		{
    22  			memory:   "7MiB",
    23  			expected: 7340032,
    24  		},
    25  		{
    26  			memory:   "8k",
    27  			expected: 8192,
    28  		},
    29  		{
    30  			memory:   "9gIb",
    31  			expected: 9663676416,
    32  		},
    33  	}
    34  	for _, tC := range testCases {
    35  		t.Run(tC.memory, func(t *testing.T) {
    36  			actual := ParseMemory(tC.memory)
    37  			if actual != tC.expected {
    38  				t.Errorf("expected %v, got %v", tC.expected, actual)
    39  			}
    40  		})
    41  	}
    42  }
    43  
    44  func TestParseEnvLine(t *testing.T) {
    45  	const FROMENV = "blah"
    46  	os.Setenv("FROMENV", FROMENV)
    47  
    48  	testCases := []struct {
    49  		line  string
    50  		key   string
    51  		value string
    52  	}{
    53  		{}, // empty line
    54  		{
    55  			line: "# comment",
    56  		},
    57  		{
    58  			line: "   # comment with leading spaces",
    59  		},
    60  		{
    61  			line:  "key=value",
    62  			key:   "key",
    63  			value: "value",
    64  		},
    65  		{
    66  			line:  "key = value", // docker errors on this, but we don't
    67  			key:   "key",
    68  			value: " value",
    69  		},
    70  		{
    71  			line:  " key=value ",
    72  			key:   "key",
    73  			value: "value ",
    74  		},
    75  		{
    76  			line:  "FROMENV",
    77  			key:   "FROMENV",
    78  			value: FROMENV,
    79  		},
    80  		{
    81  			line: "EMPTY=",
    82  			key:  "EMPTY",
    83  		},
    84  		{
    85  			line: "NONEXISTENT",
    86  		},
    87  	}
    88  	for _, tC := range testCases {
    89  		t.Run(tC.line, func(t *testing.T) {
    90  			key, value := ParseEnvLine(tC.line)
    91  			if key != tC.key || value != tC.value {
    92  				t.Errorf("expected %v=%q, got %v=%q", tC.key, tC.value, key, value)
    93  			}
    94  		})
    95  	}
    96  }
    97  
    98  func TestParseEnvFile(t *testing.T) {
    99  	env := make(map[string]string)
   100  	parseEnvFile(`# comment
   101  key=value
   102  key2= value2
   103  # comment with leading space
   104  key3=value3 # comment at end
   105  key4=crlf`+"\r\n", env)
   106  	if len(env) != 4 {
   107  		t.Errorf("expected 4 env vars, got %v", len(env))
   108  	}
   109  	if env["key"] != "value" {
   110  		t.Errorf("expected 'value', got %q", env["key"])
   111  	}
   112  	if env["key2"] != " value2" {
   113  		t.Errorf("expected ' value2', got %q", env["key2"])
   114  	}
   115  	if env["key3"] != "value3 # comment at end" {
   116  		t.Errorf("expected 'value3 # comment at end', got %q", env["key3"])
   117  	}
   118  	if env["key4"] != "crlf" {
   119  		t.Errorf("expected 'crlf', got %q", env["key4"])
   120  	}
   121  }