Skip to content

Commit

Permalink
Change location where the WSL Pro Service reads the address file
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardGomezEscandell committed Nov 8, 2023
1 parent 2bd2b85 commit 1d03b8c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 25 deletions.
6 changes: 3 additions & 3 deletions wsl-pro-service/cmd/wsl-pro-service/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func (a *App) serve(args ...option) error {
}

if len(opt.agentPortFilePath) == 0 {
localAppData, err := opt.system.LocalAppData(ctx)
home, err := opt.system.UserProfileDir(ctx)
if err != nil {
close(a.ready)
return fmt.Errorf("Could not find $env:LocalAppData: %v", err)
return fmt.Errorf("Could not find $env:UserProfile: %v", err)
}
opt.agentPortFilePath = filepath.Join(localAppData, common.LocalAppDataDir, common.ListeningPortFileName)
opt.agentPortFilePath = filepath.Join(home, common.ListeningPortFileName)
}

srv := wslinstanceservice.New(opt.system)
Expand Down
17 changes: 9 additions & 8 deletions wsl-pro-service/internal/system/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,16 +147,16 @@ func (s System) wslDistroName(ctx context.Context) (string, error) {
return fields[3], nil
}

// LocalAppData provides the path to Windows' local app data directory from WSL,
// usually `/mnt/c/Users/JohnDoe/AppData/Local`.
func (s *System) LocalAppData(ctx context.Context) (wslPath string, err error) {
// UserProfileDir provides the path to Windows' user profile directory from WSL,
// usually `/mnt/c/Users/JohnDoe/`.
func (s *System) UserProfileDir(ctx context.Context) (wslPath string, err error) {
// Find folder where windows is mounted on
cmdExe, err := s.findCmdExe()
if err != nil {
return wslPath, err
}

exe, args := s.backend.CmdExe(cmdExe, "/C", "echo %LocalAppData%")
exe, args := s.backend.CmdExe(cmdExe, "/C", "echo %UserProfile%")
//nolint:gosec //this function simply prepends the WSL path to "cmd.exe" to the args.
out, err := exec.CommandContext(ctx, exe, args...).Output()
if err != nil {
Expand All @@ -165,16 +165,17 @@ func (s *System) LocalAppData(ctx context.Context) (wslPath string, err error) {

// Path from Windows' perspective ( C:\Users\... )
// It must be converted to linux ( /mnt/c/Users/... )
localAppDataWindows := strings.TrimSpace(string(out))
winHome := strings.TrimSpace(string(out))

exe, args = s.backend.WslpathExecutable("-ua", localAppDataWindows)
exe, args = s.backend.WslpathExecutable("-ua", winHome)
//nolint:gosec //outside of tests, this function simply prepends "wslpath" to the args.
out, err = exec.CommandContext(ctx, exe, args...).Output()
if err != nil {
return wslPath, fmt.Errorf("error: %v, stdout: %s", err, string(out))
}
localAppDataLinux := strings.TrimSpace(string(out))
return s.Path(localAppDataLinux), nil

winHomeLinux := strings.TrimSpace(string(out))
return s.Path(winHomeLinux), nil
}

// Path converts an absolute path into one inside the mocked filesystem.
Expand Down
14 changes: 7 additions & 7 deletions wsl-pro-service/internal/system/system_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestInfo(t *testing.T) {
}
}

func TestLocalAppData(t *testing.T) {
func TestUserProfile(t *testing.T) {
t.Parallel()

testCases := map[string]struct {
Expand Down Expand Up @@ -170,19 +170,19 @@ func TestLocalAppData(t *testing.T) {
os.RemoveAll(cmdExePath)
}

got, err := system.LocalAppData(context.Background())
got, err := system.UserProfileDir(context.Background())
if tc.wantErr {
require.Error(t, err, "Expected LocalAppData to return an error")
require.Error(t, err, "Expected UserProfile to return an error")
return
}
require.NoError(t, err, "Expected LocalAppData to return no errors")
require.NoError(t, err, "Expected UserProfile to return no errors")

// Validating CMD path
require.Equal(t, cmdExePath, *system.CmdExeCache(), "Unexpected path for cmd.exe")

// Validating LocalAppData
wantSuffix := `/mnt/d/Users/TestUser/AppData/Local`
require.True(t, strings.HasSuffix(got, wantSuffix), "Unexpected value returned by LocalAppData.\nWant suffix: %s\nGot: %s", wantSuffix, got)
// Validating UserProfile
wantSuffix := `/mnt/d/Users/TestUser`
require.True(t, strings.HasSuffix(got, wantSuffix), "Unexpected value returned by UserProfileDir.\nWant suffix: %s\nGot: %s", wantSuffix, got)
})
}
}
Expand Down
14 changes: 7 additions & 7 deletions wsl-pro-service/internal/testutils/mock_executables.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ var (
// defaultWindowsMount is the default path used in tests to set the windows filesystem mount.
defaultWindowsMount = "/mnt/d/"

// windowsLocalAppDataDir is the default path used in tests to store Windows agent data.
windowsLocalAppDataDir = `D:\Users\TestUser\AppData\Local\`
linuxLocalAppDataDir = "/mnt/d/Users/TestUser/AppData/Local/"
// windowsUserProfileDir is the default path used in tests to store the Windows agent address file.
windowsUserProfileDir = `D:\Users\TestUser\`
linuxUserProfileDir = "/mnt/d/Users/TestUser/"

// defaultAddrFile is the default path used in tests to store the address of the Windows Agent service.
defaultAddrFile = filepath.Join(linuxLocalAppDataDir, common.LocalAppDataDir, common.ListeningPortFileName)
defaultAddrFile = filepath.Join(linuxUserProfileDir, common.ListeningPortFileName)

//go:embed filesystem_defaults/os-release
defaultOsReleaseContents []byte
Expand Down Expand Up @@ -446,7 +446,7 @@ func WslPathMock(t *testing.T) {
}

stdout, ok := map[string]string{
windowsLocalAppDataDir: linuxLocalAppDataDir,
windowsUserProfileDir: linuxUserProfileDir,
`D:\Users\TestUser\certificate`: filepath.Join(defaultWindowsMount, "Users/TestUser/certificate"),
}[argv[1]]

Expand Down Expand Up @@ -492,7 +492,7 @@ func CmdExeMock(t *testing.T) {
return exitBadUsage
}

if argv[1] != "echo %LocalAppData%" {
if argv[1] != "echo %UserProfile%" {
fmt.Fprintf(os.Stderr, "Mock not implemented for args %q\n", argv)
return exitBadUsage
}
Expand All @@ -501,7 +501,7 @@ func CmdExeMock(t *testing.T) {
return exitError
}

fmt.Fprintln(os.Stdout, windowsLocalAppDataDir)
fmt.Fprintln(os.Stdout, windowsUserProfileDir)
return exitOk
})
}
Expand Down

0 comments on commit 1d03b8c

Please sign in to comment.