addAppTests function

List<String> addAppTests(
  1. TestedApp target,
  2. List<MatrixAppTest> tests, {
  3. String? envFile,
  4. List<String> buildArguments = const [],
  5. List<String> releaseBuildArguments = const [],
  6. FileSystem fileSystem = const LocalFileSystem(),
})

Copies the files of tests into the generated app of target, with the placeholders of the files filled, writes the files that the tests generate for the app next to them, and returns the paths of the files in the app; see MatrixAppTest.directory and MatrixAppTest.generatedFiles.

Without the app of target, for an app that smf create generated outside the matrix, only {{app_name}} is filled: the MatrixAppTest.values come from an app of the matrix, and so do the files that the tests generate.

If some of the tests declare MatrixAppTest.mocks, it also writes the configuration of the tests of the app, test/flutter_test_config.dart, with which flutter test runs each test file in test/: in a setUpAll that runs before those of the file, it initializes the binding of the tests and calls the function of the mocks of each of those tests, in the order of the tests. So the mocks of every module of an app are set up for the tests of each module, which the tests of the matrix and the tests added to an app outside it get alike.

If some of the tests run the probes of the tests of the app (MatrixAppTest.readsStartProbes), such as the start check, it also writes the list of the probes of the tests, startProbesFile, in the order of the tests, each named after the directory of its tests. So the start check that the matrix analyzes in its apps and the one that CI starts on a device run the probes of the tests that go into the app. Next to it, it writes what the script of CI that starts the app on a device needs: the answers that those probes ask for, startAnswersFile; the buildArguments, the arguments of the flutter tool that build the app with its values (see MatrixEnv.toolArguments), startBuildArgumentsFile; and the releaseBuildArguments, those that a build of the app as a release takes too, startReleaseArgumentsFile; each with one on a line.

Throws a MatrixAppTestException, before it copies anything, if a test generates files without the app of target, if a file keeps a placeholder that no value fills, if two of the tests have a file at the same path, the files they generate included, if a test generates a file at a path that is no relative path in the app, if the mocks of a test are in no file of it in test/, if its probe is in no file of it in integration_test/, if the answer of a probe is no name of small letters, digits and underscores or the answer of another probe too, if a build argument is empty or has a line break, or if a test has a file at the path of the configuration of the mocks, of the list of the probes, of the answers or of either list of build arguments, which the matrix writes, or at envFile, the path of the file of the values of the app (see MatrixEnv.file), whose values the tests give through MatrixAppTest.envValues.

Implementation

List<String> addAppTests(
  TestedApp target,
  List<MatrixAppTest> tests, {
  String? envFile,
  List<String> buildArguments = const [],
  List<String> releaseBuildArguments = const [],
  FileSystem fileSystem = const LocalFileSystem(),
}) {
  final (:generated, :app) = target;
  final context = fileSystem.path;
  final (:texts, :owners) = _filesOfTests(
    tests,
    packageName: generated.name,
    app: app,
    fileSystem: fileSystem,
  );
  String matrixFile(String path, String written) =>
      _matrixFileOf(path, written, owners, fileSystem);
  if (envFile != null) {
    matrixFile(
      envFile,
      'whose values the matrix writes from those that the tests give '
      '(MatrixAppTest.envValues).',
    );
  }
  final mocks = _mocksOf(tests, owners, fileSystem);
  if (mocks.isNotEmpty) {
    final config = matrixFile(
      _testConfig,
      'which the matrix writes for the mocks of the tests.',
    );
    texts[config] = _testConfigOf(mocks);
  }
  final probes = _probesOf(tests, owners, fileSystem);
  if (tests.any((test) => test.readsStartProbes)) {
    _checkBuildArguments(startBuildArgumentsFile, buildArguments);
    _checkBuildArguments(startReleaseArgumentsFile, releaseBuildArguments);
    for (final (path, text, what) in _startFilesOf(
      probes,
      buildArguments,
      releaseBuildArguments,
    )) {
      texts[matrixFile(path, 'which the matrix writes for $what.')] = text;
    }
  }
  for (final MapEntry(key: path, value: text) in texts.entries) {
    fileSystem.file(context.join(generated.path, path))
      ..createSync(recursive: true)
      ..writeAsStringSync(text);
  }
  return [...texts.keys];
}