Use local pub in binstubs when testing (#4192)

diff --git a/lib/src/global_packages.dart b/lib/src/global_packages.dart
index 067803f..ab80d24 100644
--- a/lib/src/global_packages.dart
+++ b/lib/src/global_packages.dart
@@ -782,6 +782,10 @@
         return previousPackage;
       }
     }
+    // When running tests we want the binstub to invoke the current pub, not the
+    // one from the sdk.
+    final pubInvocation =
+        runningFromTest ? Platform.script.toFilePath() : 'pub';
 
     late String binstub;
     // We need an absolute path since relative ones won't be relative to the
@@ -809,9 +813,9 @@
   if not errorlevel 253 (
     goto error
   )
-  call dart pub global run ${package.name}:$script %*
+  call dart $pubInvocation global run ${package.name}:$script %*
 ) else (
-  call dart pub global run ${package.name}:$script %*
+  call dart $pubInvocation global run ${package.name}:$script %*
 )
 goto eof
 :error
@@ -834,9 +838,9 @@
   if [ \$exit_code != 253 ]; then
     exit \$exit_code
   fi
-  dart pub global run ${package.name}:$script "\$@"
+  dart $pubInvocation -v global run ${package.name}:$script "\$@"
 else
-  dart pub global run ${package.name}:$script "\$@"
+  dart $pubInvocation global run ${package.name}:$script "\$@"
 fi
 ''';
     }
diff --git a/test/global/binstubs/binstub_runs_global_run_if_no_snapshot_test.dart b/test/global/binstubs/binstub_runs_global_run_if_no_snapshot_test.dart
index 1d6f54e..b3a804a 100644
--- a/test/global/binstubs/binstub_runs_global_run_if_no_snapshot_test.dart
+++ b/test/global/binstubs/binstub_runs_global_run_if_no_snapshot_test.dart
@@ -27,7 +27,7 @@
       d.dir('bin', [
         d.file(
           binStubName('foo-script'),
-          contains('pub global run foo:script'),
+          contains('global run foo:script'),
         ),
       ]),
     ]).validate();
diff --git a/test/global/binstubs/explicit_executables_test.dart b/test/global/binstubs/explicit_executables_test.dart
index de899b1..04c3762 100644
--- a/test/global/binstubs/explicit_executables_test.dart
+++ b/test/global/binstubs/explicit_executables_test.dart
@@ -34,9 +34,9 @@
 
     await d.dir(cachePath, [
       d.dir('bin', [
-        d.file(binStubName('one'), contains('pub global run foo:script')),
+        d.file(binStubName('one'), contains('global run foo:script')),
         d.nothing(binStubName('two')),
-        d.file(binStubName('three'), contains('pub global run foo:script')),
+        d.file(binStubName('three'), contains('global run foo:script')),
       ]),
     ]).validate();
   });
diff --git a/test/global/binstubs/path_package_test.dart b/test/global/binstubs/path_package_test.dart
index 1b44351..6cdc37f 100644
--- a/test/global/binstubs/path_package_test.dart
+++ b/test/global/binstubs/path_package_test.dart
@@ -25,7 +25,7 @@
     await d.dir(cachePath, [
       d.dir(
         'bin',
-        [d.file(binStubName('foo'), contains('pub global run foo:foo'))],
+        [d.file(binStubName('foo'), contains('global run foo:foo'))],
       ),
     ]).validate();
   });