Upgrade other versions conservatively with --major-versions (#3295)

diff --git a/lib/src/command/upgrade.dart b/lib/src/command/upgrade.dart
index a1bd2dd..0dc5fb7 100644
--- a/lib/src/command/upgrade.dart
+++ b/lib/src/command/upgrade.dart
@@ -224,30 +224,34 @@
         resolvedPackage.version,
       ));
     }
+    final newPubspecText = _updatePubspec(changes);
 
     if (_dryRun) {
       // Even if it is a dry run, run `acquireDependencies` so that the user
       // gets a report on changes.
       await Entrypoint.inMemory(
-        Package.inMemory(resolvablePubspec),
+        Package.inMemory(
+          Pubspec.parse(newPubspecText, cache.sources),
+        ),
         cache,
         lockFile: entrypoint.lockFile,
         solveResult: solveResult,
       ).acquireDependencies(
-        SolveType.upgrade,
+        SolveType.get,
         dryRun: true,
         precompile: _precompile,
         analytics: null, // No analytics for dry-run
         generateDotPackages: false,
       );
     } else {
-      await _updatePubspec(changes);
-
+      if (changes.isNotEmpty) {
+        writeTextFile(entrypoint.pubspecPath, newPubspecText);
+      }
       // TODO: Allow Entrypoint to be created with in-memory pubspec, so that
       //       we can show the changes when not in --dry-run mode. For now we only show
       //       the changes made to pubspec.yaml in dry-run mode.
       await Entrypoint(directory, cache).acquireDependencies(
-        SolveType.upgrade,
+        SolveType.get,
         precompile: _precompile,
         analytics: analytics,
         generateDotPackages: argResults['legacy-packages-file'],
@@ -320,12 +324,13 @@
       changes[dep] = dep.withConstraint(constraint);
     }
 
+    final newPubspecText = _updatePubspec(changes);
     if (_dryRun) {
       // Even if it is a dry run, run `acquireDependencies` so that the user
       // gets a report on changes.
       // TODO(jonasfj): Stop abusing Entrypoint.global for dry-run output
       await Entrypoint.inMemory(
-        Package.inMemory(nullsafetyPubspec),
+        Package.inMemory(Pubspec.parse(newPubspecText, cache.sources)),
         cache,
         lockFile: entrypoint.lockFile,
         solveResult: solveResult,
@@ -337,8 +342,9 @@
         generateDotPackages: false,
       );
     } else {
-      await _updatePubspec(changes);
-
+      if (changes.isNotEmpty) {
+        writeTextFile(entrypoint.pubspecPath, newPubspecText);
+      }
       // TODO: Allow Entrypoint to be created with in-memory pubspec, so that
       //       we can show the changes in --dry-run mode. For now we only show
       //       the changes made to pubspec.yaml in dry-run mode.
@@ -390,13 +396,11 @@
   }
 
   /// Updates `pubspec.yaml` with given [changes].
-  Future<void> _updatePubspec(
+  String _updatePubspec(
     Map<PackageRange, PackageRange> changes,
-  ) async {
+  ) {
     ArgumentError.checkNotNull(changes, 'changes');
 
-    if (changes.isEmpty) return;
-
     final yamlEditor = YamlEditor(readTextFile(entrypoint.pubspecPath));
     final deps = entrypoint.root.pubspec.dependencies.keys;
     final devDeps = entrypoint.root.pubspec.devDependencies.keys;
@@ -416,9 +420,7 @@
         );
       }
     }
-
-    /// Windows line endings are already handled by [yamlEditor]
-    writeTextFile(entrypoint.pubspecPath, yamlEditor.toString());
+    return yamlEditor.toString();
   }
 
   /// Outputs a summary of changes made to `pubspec.yaml`.
diff --git a/test/testdata/goldens/upgrade/example_warns_about_major_versions_test/pub upgrade --major-versions does not update major versions in example~.txt b/test/testdata/goldens/upgrade/example_warns_about_major_versions_test/pub upgrade --major-versions does not update major versions in example~.txt
index 2cc96c0..ae304ea 100644
--- a/test/testdata/goldens/upgrade/example_warns_about_major_versions_test/pub upgrade --major-versions does not update major versions in example~.txt
+++ b/test/testdata/goldens/upgrade/example_warns_about_major_versions_test/pub upgrade --major-versions does not update major versions in example~.txt
@@ -17,9 +17,7 @@
 ## Section 1
 $ pub upgrade --major-versions --directory example
 Resolving dependencies in example...
-  bar 2.0.0
 > foo 2.0.0 (was 1.0.0)
-  myapp 0.0.0 from path .
 Changed 1 dependency in example!
 
 Changed 1 constraint in pubspec.yaml:
diff --git a/test/upgrade/upgrade_major_versions_test.dart b/test/upgrade/upgrade_major_versions_test.dart
index 0f0b0f6..c5f0994 100644
--- a/test/upgrade/upgrade_major_versions_test.dart
+++ b/test/upgrade/upgrade_major_versions_test.dart
@@ -99,11 +99,10 @@
     });
 
     test('upgrades only the selected package', () async {
-      await servePackages()
+      final server = await servePackages()
         ..serve('foo', '1.0.0')
         ..serve('foo', '2.0.0')
-        ..serve('bar', '0.1.0')
-        ..serve('bar', '0.2.0');
+        ..serve('bar', '0.1.0');
 
       await d.appDir({
         'foo': '^1.0.0',
@@ -112,6 +111,8 @@
 
       await pubGet();
 
+      server.serve('bar', '0.1.1');
+
       // 1 constraint should be updated
       await pubUpgrade(
         args: ['--major-versions', 'foo'],