migrator: Avoid attempting to migrate empty content (#39771)

Dino and Smit Barmase created

This commit fixes an issue where opening zed using `--user-data-dir`
with an empty directory would cause the first run to display a "Failed
to migrate settings" error.

This was caused by the migrator attempting to migrate an empty string,
so if that's the case, we'll simply return `Ok(None)` and avoid
attempting to migrate anything at all.

Relates to #39400

Release Notes:

- N/A

Co-authored-by: Smit Barmase <heysmitbarmase@gmail.com>

Change summary

crates/migrator/src/migrator.rs | 11 +++++++++++
1 file changed, 11 insertions(+)

Detailed changes

crates/migrator/src/migrator.rs 🔗

@@ -65,7 +65,13 @@ fn migrate(text: &str, patterns: MigrationPatterns, query: &Query) -> Result<Opt
     }
 }
 
+/// Runs the provided migrations on the given text.
+/// Will automatically return `Ok(None)` if there's no content to migrate.
 fn run_migrations(text: &str, migrations: &[MigrationType]) -> Result<Option<String>> {
+    if text.is_empty() {
+        return Ok(None);
+    }
+
     let mut current_text = text.to_string();
     let mut result: Option<String> = None;
     for migration in migrations.iter() {
@@ -371,6 +377,11 @@ mod tests {
         assert_migrated_correctly(migrated, output);
     }
 
+    #[test]
+    fn test_empty_content() {
+        assert_migrate_settings("", None)
+    }
+
     #[test]
     fn test_replace_array_with_single_string() {
         assert_migrate_keymap(