github.com/khulnasoft/cli@v0.0.0-20240402070845-01bcad7beefa/service/logs/parse_logs_test.go (about)

     1  package logs
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gotest.tools/v3/assert"
     7  	is "gotest.tools/v3/assert/cmp"
     8  )
     9  
    10  func TestParseLogDetails(t *testing.T) {
    11  	testCases := []struct {
    12  		line        string
    13  		expected    map[string]string
    14  		expectedErr string
    15  	}{
    16  		{
    17  			line:     "key=value",
    18  			expected: map[string]string{"key": "value"},
    19  		},
    20  		{
    21  			line:     "key1=value1,key2=value2",
    22  			expected: map[string]string{"key1": "value1", "key2": "value2"},
    23  		},
    24  		{
    25  			line:     "key+with+spaces=value%3Dequals,asdf%2C=",
    26  			expected: map[string]string{"key with spaces": "value=equals", "asdf,": ""},
    27  		},
    28  		{
    29  			line:     "key=,key2=",
    30  			expected: map[string]string{"key": "", "key2": ""},
    31  		},
    32  		{
    33  			line:        "key=,=nothing",
    34  			expectedErr: "invalid details format",
    35  		},
    36  		{
    37  			line:        "=nothing",
    38  			expectedErr: "invalid details format",
    39  		},
    40  		{
    41  			line:        "=",
    42  			expectedErr: "invalid details format",
    43  		},
    44  		{
    45  			line:        "errors",
    46  			expectedErr: "invalid details format",
    47  		},
    48  	}
    49  	for _, tc := range testCases {
    50  		tc := tc
    51  		t.Run(tc.line, func(t *testing.T) {
    52  			actual, err := ParseLogDetails(tc.line)
    53  			if tc.expectedErr != "" {
    54  				assert.Check(t, is.ErrorContains(err, tc.expectedErr))
    55  			} else {
    56  				assert.Check(t, err)
    57  			}
    58  			assert.Check(t, is.DeepEqual(tc.expected, actual))
    59  		})
    60  	}
    61  }