Skip to content

Commit 6dfa53b

Browse files
[flutter_conductor] pretty-print state JSON file (flutter#87756)
1 parent 6b32482 commit 6dfa53b

File tree

2 files changed

+73
-2
lines changed

2 files changed

+73
-2
lines changed

dev/conductor/lib/state.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// Use of this source code is governed by a BSD-style license that can be
33
// found in the LICENSE file.
44

5-
import 'dart:convert' show jsonDecode, jsonEncode;
5+
import 'dart:convert' show JsonEncoder, jsonDecode;
66

77
import 'package:file/file.dart' show File;
88
import 'package:platform/platform.dart';
@@ -230,10 +230,13 @@ ReleasePhase getNextPhase(ReleasePhase currentPhase) {
230230
return nextPhase;
231231
}
232232

233+
// Indent two spaces.
234+
const JsonEncoder _encoder = JsonEncoder.withIndent(' ');
235+
233236
void writeStateToFile(File file, pb.ConductorState state, List<String> logs) {
234237
state.logs.addAll(logs);
235238
file.writeAsStringSync(
236-
jsonEncode(state.toProto3Json()),
239+
_encoder.convert(state.toProto3Json()),
237240
flush: true,
238241
);
239242
}

dev/conductor/test/state_test.dart

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// Copyright 2014 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'package:conductor/proto/conductor_state.pb.dart' as pb;
6+
import 'package:conductor/state.dart';
7+
import 'package:file/file.dart';
8+
import 'package:file/memory.dart';
9+
10+
import './common.dart';
11+
12+
void main() {
13+
test('writeStateToFile() pretty-prints JSON with 2 spaces', () {
14+
final MemoryFileSystem fileSystem = MemoryFileSystem.test();
15+
final File stateFile = fileSystem.file('/path/to/statefile.json')
16+
..createSync(recursive: true);
17+
const String candidateBranch = 'flutter-2.3-candidate.0';
18+
final pb.ConductorState state = pb.ConductorState(
19+
releaseChannel: 'stable',
20+
releaseVersion: '2.3.4',
21+
incrementLevel: 'z',
22+
engine: pb.Repository(
23+
candidateBranch: candidateBranch,
24+
upstream: pb.Remote(
25+
name: 'upstream',
26+
url: 'https://github.com/flutter/engine.git',
27+
),
28+
),
29+
framework: pb.Repository(
30+
candidateBranch: candidateBranch,
31+
upstream: pb.Remote(
32+
name: 'upstream',
33+
url: 'https://github.com/flutter/flutter.git',
34+
),
35+
),
36+
);
37+
writeStateToFile(
38+
stateFile,
39+
state,
40+
<String>['[status] hello world'],
41+
);
42+
final String serializedState = stateFile.readAsStringSync();
43+
const String expectedString = '''
44+
{
45+
"releaseChannel": "stable",
46+
"releaseVersion": "2.3.4",
47+
"engine": {
48+
"candidateBranch": "flutter-2.3-candidate.0",
49+
"upstream": {
50+
"name": "upstream",
51+
"url": "https://github.com/flutter/engine.git"
52+
}
53+
},
54+
"framework": {
55+
"candidateBranch": "flutter-2.3-candidate.0",
56+
"upstream": {
57+
"name": "upstream",
58+
"url": "https://github.com/flutter/flutter.git"
59+
}
60+
},
61+
"logs": [
62+
"[status] hello world"
63+
],
64+
"incrementLevel": "z"
65+
}''';
66+
expect(serializedState, expectedString);
67+
});
68+
}

0 commit comments

Comments
 (0)