|
| 1 | +//go:build windows |
| 2 | + |
| 3 | +package wutil |
| 4 | + |
| 5 | +import ( |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "golang.org/x/text/encoding/unicode" |
| 8 | + "io" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 14 | + WSL1InstalledWithWSLAndVMPEnabled = `Default Version: 1` |
| 15 | + WSL2InstalledWithWSLAndVMPEnabled = `Default Version: 2` |
| 16 | + WSL1NotInstalled = `Default Version: 1 |
| 17 | +
|
| 18 | +The Windows Subsystem for Linux kernel can be manually updated with 'wsl --update', but automatic updates cannot occur due to your system settings. |
| 19 | +To receive automatic kernel updates, please enable the Windows Update setting: 'Receive updates for other Microsoft products when you update Windows'. |
| 20 | +For more information please visit https://aka.ms/wsl2kernel. |
| 21 | +
|
| 22 | +The WSL 2 kernel file is not found. To update or restore the kernel please run 'wsl --update'.` |
| 23 | + WSL2NotInstalled = `The Windows Subsystem for Linux is not installed. You can install by running 'wsl.exe --install'. |
| 24 | +For more information please visit https://aka.ms/wslinstall` |
| 25 | + WSL2InstalledWithWSLDisabled = `Default Version: 2 |
| 26 | +WSL1 is not supported with your current machine configuration. |
| 27 | +Please enable the "Windows Subsystem for Linux" optional component to use WSL1.` |
| 28 | + WSL2InstalledWithVMPDisabled = `Default Version: 2 |
| 29 | +WSL2 is not supported with your current machine configuration. |
| 30 | +Please enable the "Virtual Machine Platform" optional component and ensure virtualization is enabled in the BIOS. |
| 31 | +Enable "Virtual Machine Platform" by running: wsl.exe --install --no-distribution |
| 32 | +For information please visit https://aka.ms/enablevirtualization` |
| 33 | + WSL2InstalledWithWSLAndVMPDisabled = `Default Version: 2 |
| 34 | +WSL1 is not supported with your current machine configuration. |
| 35 | +Please enable the "Windows Subsystem for Linux" optional component to use WSL1. |
| 36 | +WSL2 is not supported with your current machine configuration. |
| 37 | +Please enable the "Virtual Machine Platform" optional component and ensure virtualization is enabled in the BIOS. |
| 38 | +Enable "Virtual Machine Platform" by running: wsl.exe --install --no-distribution |
| 39 | +For information please visit https://aka.ms/enablevirtualization` |
| 40 | + WSL1InstalledWithVMPDisabled = `Default Version: 1 |
| 41 | +Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS. |
| 42 | +For information please visit https://aka.ms/enablevirtualization` |
| 43 | + WSL1InstalledWithWSLDisabled = `Default Version: 1 |
| 44 | +WSL1 is not supported with your current machine configuration. |
| 45 | +Please enable the "Windows Subsystem for Linux" optional component to use WSL1.` |
| 46 | +) |
| 47 | + |
| 48 | +func TestMatchOutputLine(t *testing.T) { |
| 49 | + tests := []struct { |
| 50 | + winVariant string |
| 51 | + statusOutput string |
| 52 | + want wslStatus |
| 53 | + }{ |
| 54 | + { |
| 55 | + "WSL1 configured and both Virtual Machine Platform enabled and Windows Subsystem for Linux are enabled", |
| 56 | + WSL1InstalledWithWSLAndVMPEnabled, |
| 57 | + wslStatus{ |
| 58 | + installed: true, |
| 59 | + vmpFeatureEnabled: true, |
| 60 | + wslFeatureEnabled: true, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + "WSL2 configured and both Virtual Machine Platform enabled and Windows Subsystem for Linux enabled", |
| 65 | + WSL2InstalledWithWSLAndVMPEnabled, |
| 66 | + wslStatus{ |
| 67 | + installed: true, |
| 68 | + vmpFeatureEnabled: true, |
| 69 | + wslFeatureEnabled: true, |
| 70 | + }, |
| 71 | + }, |
| 72 | + { |
| 73 | + "WSL not installed (was previously configured as version 1)", |
| 74 | + WSL1NotInstalled, |
| 75 | + wslStatus{ |
| 76 | + installed: false, |
| 77 | + vmpFeatureEnabled: true, |
| 78 | + wslFeatureEnabled: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + { |
| 82 | + "WSL not installed (was previously configured as version 2)", |
| 83 | + WSL2NotInstalled, |
| 84 | + wslStatus{ |
| 85 | + installed: false, |
| 86 | + vmpFeatureEnabled: true, |
| 87 | + wslFeatureEnabled: true, |
| 88 | + }, |
| 89 | + }, |
| 90 | + { |
| 91 | + "WSL2 configured and Virtual Machine Platform is enabled but Windows Subsystem for Linux is disabled", |
| 92 | + WSL2InstalledWithWSLDisabled, |
| 93 | + wslStatus{ |
| 94 | + installed: true, |
| 95 | + vmpFeatureEnabled: true, |
| 96 | + wslFeatureEnabled: false, |
| 97 | + }, |
| 98 | + }, |
| 99 | + { |
| 100 | + "WSL2 configured and Virtual Machine Platform is disabled but Windows Subsystem for Linux is enabled", |
| 101 | + WSL2InstalledWithVMPDisabled, |
| 102 | + wslStatus{ |
| 103 | + installed: true, |
| 104 | + vmpFeatureEnabled: false, |
| 105 | + wslFeatureEnabled: true, |
| 106 | + }, |
| 107 | + }, |
| 108 | + { |
| 109 | + "WSL2 configured and both Virtual Machine Platform and Windows Subsystem for Linux are disabled", |
| 110 | + WSL2InstalledWithWSLAndVMPDisabled, |
| 111 | + wslStatus{ |
| 112 | + installed: true, |
| 113 | + vmpFeatureEnabled: false, |
| 114 | + wslFeatureEnabled: false, |
| 115 | + }, |
| 116 | + }, |
| 117 | + { |
| 118 | + "WSL1 configured and Virtual Machine Platform is disabled but Windows Subsystem for Linux is enabled", |
| 119 | + WSL1InstalledWithVMPDisabled, |
| 120 | + wslStatus{ |
| 121 | + installed: true, |
| 122 | + vmpFeatureEnabled: false, |
| 123 | + wslFeatureEnabled: true, |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + "WSL1 configured and Virtual Machine Platform is enabled but Windows Subsystem for Linux is disabled", |
| 128 | + WSL1InstalledWithWSLDisabled, |
| 129 | + wslStatus{ |
| 130 | + installed: true, |
| 131 | + vmpFeatureEnabled: true, |
| 132 | + wslFeatureEnabled: false, |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + for _, tt := range tests { |
| 137 | + t.Run(tt.winVariant, func(t *testing.T) { |
| 138 | + encoder := unicode.UTF16(unicode.LittleEndian, unicode.UseBOM).NewEncoder() |
| 139 | + encodedOutput, err := encoder.String(tt.statusOutput) |
| 140 | + assert.Nil(t, err) |
| 141 | + reader := io.NopCloser(strings.NewReader(encodedOutput)) |
| 142 | + assert.Equal(t, tt.want, matchOutputLine(reader)) |
| 143 | + }) |
| 144 | + } |
| 145 | +} |
0 commit comments