<commit_before>// Copyright 2016 The Chromium 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 'dart:async';

import 'package:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/devfs.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import 'src/context.dart';
import 'src/mocks.dart';

void main() {
  String filePath = 'bar/foo.txt';
  String filePath2 = 'foo/bar.txt';
  Directory tempDir;
  String basePath;
  MockDevFSOperations devFSOperations = new MockDevFSOperations();
  DevFS devFS;
  AssetBundle assetBundle = new AssetBundle();
  assetBundle.entries.add(new AssetBundleEntry.fromString('a.txt', ''));
  group('devfs', () {
    testUsingContext('create local file system', () async {
      tempDir = Directory.systemTemp.createTempSync();
      basePath = tempDir.path;
      File file = new File(path.join(basePath, filePath));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);
    });
    testUsingContext('create dev file system', () async {
      devFS = new DevFS.operations(devFSOperations, 'test', tempDir);
      await devFS.create();
      expect(devFSOperations.contains('create test'), isTrue);
    });
    testUsingContext('populate dev file system', () async {
      await devFS.update();
      expect(devFSOperations.contains('writeFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('add new file to local file system', () async {
      File file = new File(path.join(basePath, filePath2));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3, 4, 5, 6, 7]);
      await devFS.update();
      expect(devFSOperations.contains('writeFile test foo/bar.txt'), isTrue);
      // Need to delay between when we first write to a file and when
      // we modify it.
      await new Future<Null>.delayed(new Duration(seconds: 1));
    });
    testUsingContext('modify existing file on local file system', () async {
      File file = new File(path.join(basePath, filePath));
      await file.writeAsBytes(<int>[1, 2, 3, 4, 5, 6]);
      await devFS.update();
      expect(devFSOperations.contains('writeFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('delete a file from the local file system', () async {
      File file = new File(path.join(basePath, filePath));
      await file.delete();
      await devFS.update();
      expect(devFSOperations.contains('deleteFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('add file in an asset bundle', () async {
      await devFS.update(bundle: assetBundle, bundleDirty: true);
      expect(devFSOperations.contains(
          'writeFile test ${getAssetBuildDirectory()}/a.txt'), isTrue);
    });
    testUsingContext('add a file to the asset bundle', () async {
      assetBundle.entries.add(new AssetBundleEntry.fromString('b.txt', ''));
      await devFS.update(bundle: assetBundle, bundleDirty: true);
      expect(devFSOperations.contains(
          'writeFile test ${getAssetBuildDirectory()}/b.txt'), isTrue);
    });
    testUsingContext('delete a file from the asset bundle', () async {
      assetBundle.entries.clear();
      await devFS.update(bundle: assetBundle, bundleDirty: true);
      expect(devFSOperations.contains(
          'deleteFile test ${getAssetBuildDirectory()}/b.txt'), isTrue);
    });
    testUsingContext('delete dev file system', () async {
      await devFS.destroy();
    });
  });
}
<commit_msg>Use touch to avoid race condition in file modification test (#7117)<commit_after>// Copyright 2016 The Chromium 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:flutter_tools/src/asset.dart';
import 'package:flutter_tools/src/build_info.dart';
import 'package:flutter_tools/src/devfs.dart';
import 'package:path/path.dart' as path;
import 'package:test/test.dart';

import 'src/context.dart';
import 'src/mocks.dart';

void main() {
  String filePath = 'bar/foo.txt';
  String filePath2 = 'foo/bar.txt';
  Directory tempDir;
  String basePath;
  MockDevFSOperations devFSOperations = new MockDevFSOperations();
  DevFS devFS;
  AssetBundle assetBundle = new AssetBundle();
  assetBundle.entries.add(new AssetBundleEntry.fromString('a.txt', ''));
  group('devfs', () {
    testUsingContext('create local file system', () async {
      tempDir = Directory.systemTemp.createTempSync();
      basePath = tempDir.path;
      File file = new File(path.join(basePath, filePath));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3]);
    });
    testUsingContext('create dev file system', () async {
      devFS = new DevFS.operations(devFSOperations, 'test', tempDir);
      await devFS.create();
      expect(devFSOperations.contains('create test'), isTrue);
    });
    testUsingContext('populate dev file system', () async {
      await devFS.update();
      expect(devFSOperations.contains('writeFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('add new file to local file system', () async {
      File file = new File(path.join(basePath, filePath2));
      await file.parent.create(recursive: true);
      file.writeAsBytesSync(<int>[1, 2, 3, 4, 5, 6, 7]);
      await devFS.update();
      expect(devFSOperations.contains('writeFile test foo/bar.txt'), isTrue);
    });
    testUsingContext('modify existing file on local file system', () async {
      File file = new File(path.join(basePath, filePath));
      // Set the last modified time to 5 seconds ago.
      Process.runSync('touch', <String>['-A', '-05', file.path]);
      await devFS.update();
      await file.writeAsBytes(<int>[1, 2, 3, 4, 5, 6]);
      await devFS.update();
      expect(devFSOperations.contains('writeFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('delete a file from the local file system', () async {
      File file = new File(path.join(basePath, filePath));
      await file.delete();
      await devFS.update();
      expect(devFSOperations.contains('deleteFile test bar/foo.txt'), isTrue);
    });
    testUsingContext('add file in an asset bundle', () async {
      await devFS.update(bundle: assetBundle, bundleDirty: true);
      expect(devFSOperations.contains(
          'writeFile test ${getAssetBuildDirectory()}/a.txt'), isTrue);
    });
    testUsingContext('add a file to the asset bundle', () async {
      assetBundle.entries.add(new AssetBundleEntry.fromString('b.txt', ''));
      await devFS.update(bundle: assetBundle, bundleDirty: true);
      expect(devFSOperations.contains(
          'writeFile test ${getAssetBuildDirectory()}/b.txt'), isTrue);
    });
    testUsingContext('delete a file from the asset bundle', () async {
      assetBundle.entries.clear();
      await devFS.update(bundle: assetBundle, bundleDirty: true);
      expect(devFSOperations.contains(
          'deleteFile test ${getAssetBuildDirectory()}/b.txt'), isTrue);
    });
    testUsingContext('delete dev file system', () async {
      await devFS.destroy();
    });
  });
}

{'fraction_non_alphanumeric': 0.13833883388338833, 'fraction_numerical': 0.007150715071507151, 'mean_word_length': 3.7104922279792745, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7309987', 'n_tokens_mistral': 2208, 'n_tokens_neox': 2189, 'n_words': 562}