post_process_docs_test.dart 5.38 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

import 'dart:io';

import 'package:file/memory.dart';
import 'package:platform/platform.dart';

import '../../../packages/flutter_tools/test/src/fake_process_manager.dart';
import '../post_process_docs.dart';
import 'common.dart';

void main() async {
  group('getBranch', () {
    const String branchName = 'stable';
    test('getBranchName does not call git if env LUCI_BRANCH provided', () async {
      final Platform platform = FakePlatform(
        environment: <String, String>{
          'LUCI_BRANCH': branchName,
        },
      );
      final ProcessManager processManager = FakeProcessManager.empty();
      final String calculatedBranchName = await getBranchName(
        platform: platform,
        processManager: processManager,
      );
      expect(calculatedBranchName, branchName);
    });

    test('getBranchName calls git if env LUCI_BRANCH not provided', () async {
      final Platform platform = FakePlatform(
        environment: <String, String>{},
      );

      final ProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          const FakeCommand(
            command: <String>['git', 'status', '-b', '--porcelain'],
            stdout: '## $branchName',
          ),
        ],
      );

      final String calculatedBranchName = await getBranchName(platform: platform, processManager: processManager);
      expect(
        calculatedBranchName,
        branchName,
      );
      expect(processManager, hasNoRemainingExpectations);
    });
    test('getBranchName calls git if env LUCI_BRANCH is empty', () async {
      final Platform platform = FakePlatform(
        environment: <String, String>{
          'LUCI_BRANCH': '',
        },
      );

      final ProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          const FakeCommand(
            command: <String>['git', 'status', '-b', '--porcelain'],
            stdout: '## $branchName',
          ),
        ],
      );
      final String calculatedBranchName = await getBranchName(
        platform: platform,
        processManager: processManager,
      );
      expect(
        calculatedBranchName,
        branchName,
      );
      expect(processManager, hasNoRemainingExpectations);
    });
  });

  group('gitRevision', () {
    test('Return short format', () async {
      const String commitHash = 'e65f01793938e13cac2d321b9fcdc7939f9b2ea6';
      final ProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          const FakeCommand(
            command: <String>['git', 'rev-parse', 'HEAD'],
            stdout: commitHash,
          ),
        ],
      );
      final String revision = await gitRevision(processManager: processManager);
      expect(processManager, hasNoRemainingExpectations);
      expect(revision, commitHash.substring(0, 10));
    });

    test('Return full length', () async {
      const String commitHash = 'e65f01793938e13cac2d321b9fcdc7939f9b2ea6';
      final ProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          const FakeCommand(
            command: <String>['git', 'rev-parse', 'HEAD'],
            stdout: commitHash,
          ),
        ],
      );
      final String revision = await gitRevision(fullLength: true, processManager: processManager);
      expect(processManager, hasNoRemainingExpectations);
      expect(revision, commitHash);
    });
  });

  group('runProcessWithValidation', () {
    test('With no error', () async {
      const List<String> command = <String>['git', 'rev-parse', 'HEAD'];
      final ProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          const FakeCommand(
            command: command,
          ),
        ],
      );
121
      await runProcessWithValidations(command, '', processManager: processManager, verbose: false);
122 123 124 125 126 127 128 129 130 131 132 133 134 135
      expect(processManager, hasNoRemainingExpectations);
    });

    test('With error', () async {
      const List<String> command = <String>['git', 'rev-parse', 'HEAD'];
      final ProcessManager processManager = FakeProcessManager.list(
        <FakeCommand>[
          const FakeCommand(
            command: command,
            exitCode: 1,
          ),
        ],
      );
      try {
136
        await runProcessWithValidations(command, '', processManager: processManager, verbose: false);
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
        throw Exception('Exception was not thrown');
      } on CommandException catch (e) {
        expect(e, isA<Exception>());
      }
    });
  });

  group('generateFooter', () {
    test('generated correctly', () async {
      const String expectedContent = '''
(function() {
  var span = document.querySelector('footer>span');
  if (span) {
    span.innerText = 'Flutter 3.0.0  2022-09-22 14:09  abcdef  stable';
  }
  var sourceLink = document.querySelector('a.source-link');
  if (sourceLink) {
    sourceLink.href = sourceLink.href.replace('/master/', '/abcdef/');
  }
})();
''';
      final MemoryFileSystem fs = MemoryFileSystem();
      final File footerFile = fs.file('/a/b/c/footer.js')..createSync(recursive: true);
      await createFooter(footerFile, '3.0.0', timestampParam: '2022-09-22 14:09', branchParam: 'stable', revisionParam: 'abcdef');
      final String content = await footerFile.readAsString();
      expect(content, expectedContent);
    });
  });
}