github.com/hashicorp/packer@v1.14.3/internal/hcp/registry/metadata/os_test.go (about)

     1  package metadata
     2  
     3  import (
     4  	"fmt"
     5  	"runtime"
     6  	"testing"
     7  )
     8  
     9  // MockExecutor is a mock implementation of CommandExecutor.
    10  type MockExecutor struct {
    11  	stdout string
    12  	err    error
    13  }
    14  
    15  // Exec returns a mocked output.
    16  func (m MockExecutor) Exec(name string, arg ...string) ([]byte, error) {
    17  	return []byte(m.stdout), m.err
    18  }
    19  
    20  func TestGetInfoForWindows(t *testing.T) {
    21  	tests := []struct {
    22  		name     string
    23  		stdout   string
    24  		err      error
    25  		expected OSInfo
    26  	}{
    27  		{
    28  			name:   "Valid version info",
    29  			stdout: "Microsoft Windows [Version 10.0.19042.928]",
    30  			err:    nil,
    31  			expected: OSInfo{
    32  				Name:    runtime.GOOS,
    33  				Arch:    runtime.GOARCH,
    34  				Version: "10.0.19042.928",
    35  			},
    36  		},
    37  		{
    38  			name:   "Invalid version info",
    39  			stdout: "Invalid output",
    40  			err:    fmt.Errorf("Invalid output"),
    41  			expected: OSInfo{
    42  				Name:    runtime.GOOS,
    43  				Arch:    runtime.GOARCH,
    44  				Version: "",
    45  			},
    46  		},
    47  	}
    48  
    49  	for _, tt := range tests {
    50  		t.Run(tt.name, func(t *testing.T) {
    51  
    52  			mockExecutor := MockExecutor{
    53  				stdout: tt.stdout,
    54  				err:    tt.err,
    55  			}
    56  
    57  			result := GetInfoForWindows(mockExecutor)
    58  
    59  			if result != tt.expected {
    60  				t.Errorf("expected %+v, got %+v", tt.expected, result)
    61  			}
    62  		})
    63  	}
    64  }