diff --git a/scripts/installer-migration.iss b/scripts/installer-migration.iss index 07f0288de..a4401d45d 100644 --- a/scripts/installer-migration.iss +++ b/scripts/installer-migration.iss @@ -318,9 +318,31 @@ begin Result := CompareText(Copy(Command, 1, Length(Prefix)), Prefix) = 0; end; +function IsSingleCommandVerb(Key: string): Boolean; +var + Children: TArrayOfString; +begin + Result := RegGetSubkeyNames(HKCU, Key, Children); + if Result then + Result := (GetArrayLength(Children) = 1) and + (CompareText(Children[0], 'command') = 0); + if Result then + Result := RegGetSubkeyNames(HKCU, Key + '\command', Children) and + (GetArrayLength(Children) = 0); +end; + +function IsOwnedWslVerb(Key, Executable: string): Boolean; +var + Command: string; +begin + Result := IsSingleCommandVerb(Key) and + RegQueryStringValue(HKCU, Key + '\command', '', Command) and + IsOwnedWslCommand(Command, Executable); +end; + function IsOwnedWslMenu(Key, Executable: string): Boolean; var - Owner, Command: string; + Owner: string; Children, Subkeys: TArrayOfString; Index: Integer; begin @@ -337,8 +359,7 @@ begin if not RegGetSubkeyNames(HKCU, Key + '\shell', Children) then Exit; for Index := 0 to GetArrayLength(Children) - 1 do begin - if not RegQueryStringValue(HKCU, Key + '\shell\' + Children[Index] + '\command', '', Command) or - not IsOwnedWslCommand(Command, Executable) then + if not IsOwnedWslVerb(Key + '\shell\' + Children[Index], Executable) then Exit; end; end; @@ -347,7 +368,7 @@ end; procedure RemoveOwnedWslContextMenusAt(Root, Executable: string); var - Key, Command: string; + Key: string; Names: TArrayOfString; NameIndex: Integer; begin @@ -355,10 +376,7 @@ begin for NameIndex := 0 to GetArrayLength(Names) - 1 do if Pos('PebrelWsl', Names[NameIndex]) = 1 then begin Key := Root + '\' + Names[NameIndex]; - Command := ''; - if IsOwnedWslMenu(Key, Executable) or - (RegQueryStringValue(HKCU, Key + '\command', '', Command) and - IsOwnedWslCommand(Command, Executable)) then + if IsOwnedWslMenu(Key, Executable) or IsOwnedWslVerb(Key, Executable) then if not RegDeleteKeyIncludingSubkeys(HKCU, Key) then RaiseException('Unable to remove an owned WSL context menu: ' + Key); end; diff --git a/scripts/tests/installer-migration-fixture.iss b/scripts/tests/installer-migration-fixture.iss index 8052f546c..2029c9246 100644 --- a/scripts/tests/installer-migration-fixture.iss +++ b/scripts/tests/installer-migration-fixture.iss @@ -185,6 +185,39 @@ begin Check(FileExists(NewLink), 'shortcut pointing at another program is preserved'); end; +procedure CheckEditedWslVerbs(ShellRoot, Menu, Executable, DirectoryArgument: string; + Distros: TArrayOfString); +var + Depth: Integer; + Verb, Flat, Suffix, Value: string; + Failed: Boolean; +begin + Verb := Menu + '\shell\PebrelWsl0'; + Flat := ShellRoot + '\PebrelWslLegacy'; + for Depth := 0 to 1 do begin + if Depth = 0 then Suffix := '\Custom' else Suffix := '\command\Custom'; + Check(RegWriteStringValue(HKCU, Verb + Suffix, '', 'keep'), 'seed edited cascade verb'); + Check(RegWriteStringValue(HKCU, Flat + '\command', '', + '"' + Executable + '" --gpui --shell "wsl:Old" --working-directory "' + + DirectoryArgument + '"'), 'seed legacy verb'); + Check(RegWriteStringValue(HKCU, Flat + Suffix, '', 'keep'), 'seed edited legacy verb'); + Failed := False; + try + RegisterWslContextMenuAt(ShellRoot, Executable, DirectoryArgument, Distros); + except + Failed := True; + end; + Check(Failed, 'registration must reject an edited WSL verb'); + RemoveOwnedWslContextMenusAt(ShellRoot, Executable); + Check(RegQueryStringValue(HKCU, Verb + Suffix, '', Value) and (Value = 'keep'), + 'preserve nested user data inside cascading verbs'); + Check(RegQueryStringValue(HKCU, Flat + Suffix, '', Value) and (Value = 'keep'), + 'preserve nested user data inside legacy verbs'); + Check(RegDeleteKeyIncludingSubkeys(HKCU, Verb + Suffix), 'remove cascade fixture edit'); + Check(RegDeleteKeyIncludingSubkeys(HKCU, Flat), 'remove legacy fixture edit'); + end; +end; + procedure CheckContextMenus(Root: string); var RegistryRoot, ShellRoot, Executable, Value, Menu, ForeignCommand: string; @@ -212,6 +245,7 @@ begin Check(not RegKeyExists(HKCU, ShellRoot + '\PebrelWsl0'), 'migrate old flat entry'); Check(RegGetSubkeyNames(HKCU, ShellRoot, Names) and (GetArrayLength(Names) = 3), 'only normal, cascade and foreign roots remain'); Check(RegGetSubkeyNames(HKCU, Menu + '\shell', Names) and (GetArrayLength(Names) = 3), 'three distros live inside cascade'); + CheckEditedWslVerbs(ShellRoot, Menu, Executable, Value, Distros); Check(RegQueryStringValue(HKCU, Menu, 'SubCommands', Value) and (Value = ''), 'enable static cascade'); Check(RegQueryStringValue(HKCU, Menu + '\shell\PebrelWsl1\command', '', Value), 'child command exists'); if Index = 0 then