[roll] Fix version file generation and improve tests

Only the last line of the VERSION file was actually written and this
wasn't covered by tests. Now the tests also check that the correct
VERSION file is written.

https://github.com/dart-lang/sdk/issues/42917

Change-Id: Ib4cfa7548ed123e309428276eaaface2391df47c
Reviewed-on: https://dart-review.googlesource.com/c/recipes/+/158041
Reviewed-by: Alexander Aprelev <aam@google.com>
Commit-Queue: Alexander Thomas <athom@google.com>
diff --git a/recipes/roller/roll_to_dev.expected/push.json b/recipes/roller/roll_to_dev.expected/push.json
index 4921d24..a41cd68 100644
--- a/recipes/roller/roll_to_dev.expected/push.json
+++ b/recipes/roller/roll_to_dev.expected/push.json
@@ -242,13 +242,20 @@
       "--json-output",
       "/path/to/tmp/json",
       "copy",
-      "UNKNOWN_FIELD cde",
+      "# Updated Comment\nCHANNEL dev\nMAJOR 4\nMINOR 1\nPATCH 2\nPRERELEASE 35\nPRERELEASE_PATCH 0\nUNKNOWN_FIELD cde",
       "[START_DIR]/sdk/tools/VERSION"
     ],
     "cwd": "[START_DIR]/sdk",
     "infra_step": true,
     "name": "write new version file",
     "~followup_annotations": [
+      "@@@STEP_LOG_LINE@VERSION@# Updated Comment@@@",
+      "@@@STEP_LOG_LINE@VERSION@CHANNEL dev@@@",
+      "@@@STEP_LOG_LINE@VERSION@MAJOR 4@@@",
+      "@@@STEP_LOG_LINE@VERSION@MINOR 1@@@",
+      "@@@STEP_LOG_LINE@VERSION@PATCH 2@@@",
+      "@@@STEP_LOG_LINE@VERSION@PRERELEASE 35@@@",
+      "@@@STEP_LOG_LINE@VERSION@PRERELEASE_PATCH 0@@@",
       "@@@STEP_LOG_LINE@VERSION@UNKNOWN_FIELD cde@@@",
       "@@@STEP_LOG_END@VERSION@@@"
     ]
diff --git a/recipes/roller/roll_to_dev.py b/recipes/roller/roll_to_dev.py
index b9dce15..419b642 100644
--- a/recipes/roller/roll_to_dev.py
+++ b/recipes/roller/roll_to_dev.py
@@ -158,7 +158,7 @@
       updated_lines.append(line)
     else:
       field, _ = line.split(' ')
-      updated_lines = ['%s %s' % (field, new_version[field])]
+      updated_lines.append('%s %s' % (field, new_version[field]))
   api.file.write_text('write new version file',
                       api.path['checkout'].join(VERSION_PATH),
                       '\n'.join(updated_lines))
@@ -187,7 +187,7 @@
           'abcdef', 'Subject\n\nMessage\n', new_files=['foo/bar', 'baz/qux']))
   from_ref_version_file = api.step_data(
       'download from_ref version file',
-      api.gitiles.make_encoded_file('''# Comment
+      api.gitiles.make_encoded_file('''# Updated Comment
 CHANNEL abc
 MAJOR 4
 MINOR 1
@@ -216,6 +216,16 @@
       api.post_process(post_process.StatusSuccess),
   )
 
+  expected_version_file = api.post_process(
+      post_process.LogEquals, 'write new version file', 'VERSION',
+      '''# Updated Comment
+CHANNEL dev
+MAJOR 4
+MINOR 1
+PATCH 2
+PRERELEASE 35
+PRERELEASE_PATCH 0
+UNKNOWN_FIELD cde''')
   unmerged = api.step_data('check if commit has been merged before', retcode=1)
   yield api.test(
       'push',
@@ -224,6 +234,7 @@
       from_ref_version_file,
       to_ref_version_file,
       unmerged,
+      expected_version_file,
       api.post_process(post_process.MustRun, 'push to refs/heads/dev'),
       api.post_process(post_process.MustRun, 'tag 4.1.2-35.0.dev'),
       api.post_process(post_process.StatusSuccess),
@@ -236,6 +247,7 @@
       from_ref_version_file,
       to_ref_version_file,
       unmerged,
+      expected_version_file,
       api.post_process(post_process.MustRun, 'push to refs/heads/dev'),
       api.post_process(post_process.MustRun, 'tag 4.1.2-35.0.dev'),
       api.post_process(post_process.StepCommandContains,
@@ -248,7 +260,7 @@
 
   from_ref_version_file = api.step_data(
       'download from_ref version file',
-      api.gitiles.make_encoded_file('''# Comment
+      api.gitiles.make_encoded_file('''# Updated Comment
 CHANNEL abc
 MAJOR 5
 MINOR 2
@@ -257,6 +269,16 @@
 PRERELEASE_PATCH 0
 UNKNOWN_FIELD cde
 '''))
+  expected_version_file = api.post_process(
+      post_process.LogEquals, 'write new version file', 'VERSION',
+      '''# Updated Comment
+CHANNEL dev
+MAJOR 5
+MINOR 2
+PATCH 10
+PRERELEASE 0
+PRERELEASE_PATCH 0
+UNKNOWN_FIELD cde''')
   yield api.test(
       'push-version-mismatch',
       input_properties,
@@ -264,6 +286,7 @@
       from_ref_version_file,
       to_ref_version_file,
       unmerged,
+      expected_version_file,
       api.post_process(post_process.MustRun, 'push to refs/heads/dev'),
       api.post_process(post_process.MustRun, 'tag 5.2.10-0.0.dev'),
       api.post_process(post_process.StatusSuccess),