-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved WslFlags functionality and tests to internal package
This avoids circular dependencies between Flags, Configuration, and Configuration-related wslApi.dll calls.
- Loading branch information
1 parent
48f6456
commit d26ef0e
Showing
5 changed files
with
151 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package flags_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"github.com/ubuntu/gowsl/internal/flags" | ||
) | ||
|
||
func TestUnpackFlags(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
wants flags.Unpacked | ||
input flags.WslFlags | ||
}{ | ||
{input: flags.WslFlags(0x0), wants: flags.Unpacked{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0x1), wants: flags.Unpacked{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0x2), wants: flags.Unpacked{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0x3), wants: flags.Unpacked{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0x4), wants: flags.Unpacked{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}}, | ||
{input: flags.WslFlags(0x5), wants: flags.Unpacked{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}}, | ||
{input: flags.WslFlags(0x6), wants: flags.Unpacked{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}}, | ||
{input: flags.WslFlags(0x7), wants: flags.Unpacked{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}}, | ||
// The following may be encountered due to an undocumented fourth flagcd | ||
{input: flags.WslFlags(0x8), wants: flags.Unpacked{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0x9), wants: flags.Unpacked{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0xa), wants: flags.Unpacked{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0xb), wants: flags.Unpacked{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}}, | ||
{input: flags.WslFlags(0xc), wants: flags.Unpacked{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}}, | ||
{input: flags.WslFlags(0xd), wants: flags.Unpacked{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}}, | ||
{input: flags.WslFlags(0xe), wants: flags.Unpacked{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}}, | ||
{input: flags.WslFlags(0xf), wants: flags.Unpacked{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(fmt.Sprintf("input_0x%x", int(tc.input)), func(t *testing.T) { | ||
t.Parallel() | ||
got := flags.Unpack(tc.input) | ||
assert.Equal(t, tc.wants.InteropEnabled, got.InteropEnabled, "InteropEnabled does not match the expected value") | ||
assert.Equal(t, tc.wants.PathAppended, got.PathAppended, "PathAppended does not match the expected value") | ||
assert.Equal(t, tc.wants.DriveMountingEnabled, got.DriveMountingEnabled, "DriveMountingEnabled does not match the expected value") | ||
}) | ||
} | ||
} | ||
|
||
func TestPackFlags(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
input flags.Unpacked | ||
wants flags.WslFlags | ||
}{ | ||
{wants: flags.WslFlags(0x0), input: flags.Unpacked{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: false}}, | ||
{wants: flags.WslFlags(0x1), input: flags.Unpacked{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: false}}, | ||
{wants: flags.WslFlags(0x2), input: flags.Unpacked{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: false}}, | ||
{wants: flags.WslFlags(0x3), input: flags.Unpacked{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: false}}, | ||
{wants: flags.WslFlags(0x4), input: flags.Unpacked{InteropEnabled: false, PathAppended: false, DriveMountingEnabled: true}}, | ||
{wants: flags.WslFlags(0x5), input: flags.Unpacked{InteropEnabled: true, PathAppended: false, DriveMountingEnabled: true}}, | ||
{wants: flags.WslFlags(0x6), input: flags.Unpacked{InteropEnabled: false, PathAppended: true, DriveMountingEnabled: true}}, | ||
{wants: flags.WslFlags(0x7), input: flags.Unpacked{InteropEnabled: true, PathAppended: true, DriveMountingEnabled: true}}, | ||
} | ||
|
||
for _, tc := range tests { | ||
tc := tc | ||
t.Run(fmt.Sprintf("expects_0x%x", int(tc.wants)), func(t *testing.T) { | ||
t.Parallel() | ||
got, _ := tc.input.Pack() | ||
require.Equal(t, tc.wants, got) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package flags | ||
|
||
import "fmt" | ||
|
||
// Unpacked contains the same information as WslFlags but in a struct instead of an integer. | ||
type Unpacked struct { | ||
InteropEnabled bool // Whether interop with windows is enabled | ||
PathAppended bool // Whether Windows paths are appended | ||
DriveMountingEnabled bool // Whether drive mounting is enabled | ||
UndocumentedWSLVersion uint8 // Undocumented variable. WSL1 vs. WSL2. | ||
} | ||
|
||
// Unpack examines a WslFlags object and stores its data in a Unpacked flags struct. | ||
func Unpack(f WslFlags) Unpacked { | ||
var up Unpacked | ||
|
||
up.InteropEnabled = false | ||
if f&flag_ENABLE_INTEROP != 0 { | ||
up.InteropEnabled = true | ||
} | ||
|
||
up.PathAppended = false | ||
if f&flag_APPEND_NT_PATH != 0 { | ||
up.PathAppended = true | ||
} | ||
|
||
up.DriveMountingEnabled = false | ||
if f&flag_ENABLE_DRIVE_MOUNTING != 0 { | ||
up.DriveMountingEnabled = true | ||
} | ||
|
||
up.UndocumentedWSLVersion = 1 | ||
if f&flag_undocumented_WSL_VERSION != 0 { | ||
up.UndocumentedWSLVersion = 2 | ||
} | ||
|
||
return up | ||
} | ||
|
||
// Pack generates a WslFlags object from the Unpacked struct. | ||
func (conf Unpacked) Pack() (WslFlags, error) { | ||
var f WslFlags | ||
|
||
if conf.InteropEnabled { | ||
f = f | flag_ENABLE_INTEROP | ||
} | ||
|
||
if conf.PathAppended { | ||
f = f | flag_APPEND_NT_PATH | ||
} | ||
|
||
if conf.DriveMountingEnabled { | ||
f = f | flag_ENABLE_DRIVE_MOUNTING | ||
} | ||
|
||
switch conf.UndocumentedWSLVersion { | ||
case 1: | ||
case 2: | ||
f = f | flag_undocumented_WSL_VERSION | ||
default: | ||
return f, fmt.Errorf("unknown WSL version %d", conf.UndocumentedWSLVersion) | ||
} | ||
|
||
return f, nil | ||
} |
This file was deleted.
Oops, something went wrong.