<commit_before>import path from 'path';
import jestSnapshot from 'jest-snapshot';
import ReactTestRenderer from 'react-test-renderer';
import diff from 'jest-diff';
import promptly from 'promptly';

export default class SnapshotRunner {
  constructor(configDir) {
    this.configDir = configDir;
    this.kind = '';
  }

  startKind(kind) {
    const filePath = path.resolve(this.configDir, kind);

    const fakeJasmine = {
      Spec: () => {}
    };
    this.state = jestSnapshot.getSnapshotState(fakeJasmine, filePath);
    this.kind = kind;
  }

  async runStory(story, {update, interactive}) {
    this.state.setSpecName(story.name);
    this.state.setCounter(0);
    const snapshot = this.state.snapshot;

    const key = story.name;
    const hasSnapshot = snapshot.has(key);
    const context = { kind: this.kind, story };
    const tree = story.render(context);
    const renderer = ReactTestRenderer.create(tree);
    const actual = renderer.toJSON();

    if (!snapshot.fileExists() || !hasSnapshot) {
      // If the file does not exist of snapshot of this name is not present
      // add it.
      snapshot.add(key, actual);
      return {state: 'added'};
    }

    const matches = snapshot.matches(key, actual);
    const pass = matches.pass;
    if (pass) {
      // Snapshot matches with the story
      return {state: 'matched'};
    }

    // Snapshot does not match story
    if (update) {
      snapshot.add(key, actual);
      return {state: 'updated'};
    }

    const diffMessage = diff(
      matches.expected.trim(),
      matches.actual.trim(),
      {
        aAnnotation: 'Snapshot',
        bAnnotation: 'Current story',
      },
    );

    if (interactive) {
      const shouldUpdate = await this.confirmUpate(diffMessage);
      if (shouldUpdate) {
        snapshot.add(key, actual);
        return {state: 'updated'};
      }
    }

    return {state: 'unmatched', message: diffMessage};
  }

  endKind({update}) {
    const snapshot = this.state.snapshot;
    if (update) {
      snapshot.removeUncheckedKeys();
    }
    snapshot.save(update);
  }

  async confirmUpate(diffMessage) {
    process.stdout.write('\nReceived story is different from stored snapshot.\n');
    process.stdout.write('  ' + diffMessage.split('\n').join('\n  '));
    let ans = await promptly.prompt('Should this snapshot be updated?(y/n)');
    while (ans !== 'y' && ans !== 'n') {
      process.stdout.write('Enter only y (yes) or n (no)\n');
      ans = await promptly.prompt('Should this snapshot be updated?(y/n)');
    }
    process.stdout.write('\n');

    return ans === 'y';
  }
}<commit_msg>Fix a typo<commit_after>import path from 'path';
import jestSnapshot from 'jest-snapshot';
import ReactTestRenderer from 'react-test-renderer';
import diff from 'jest-diff';
import promptly from 'promptly';

export default class SnapshotRunner {
  constructor(configDir) {
    this.configDir = configDir;
    this.kind = '';
  }

  startKind(kind) {
    const filePath = path.resolve(this.configDir, kind);

    const fakeJasmine = {
      Spec: () => {}
    };
    this.state = jestSnapshot.getSnapshotState(fakeJasmine, filePath);
    this.kind = kind;
  }

  async runStory(story, {update, interactive}) {
    this.state.setSpecName(story.name);
    this.state.setCounter(0);
    const snapshot = this.state.snapshot;

    const key = story.name;
    const hasSnapshot = snapshot.has(key);
    const context = { kind: this.kind, story };
    const tree = story.render(context);
    const renderer = ReactTestRenderer.create(tree);
    const actual = renderer.toJSON();

    if (!snapshot.fileExists() || !hasSnapshot) {
      // If the file does not exist of snapshot of this name is not present
      // add it.
      snapshot.add(key, actual);
      return {state: 'added'};
    }

    const matches = snapshot.matches(key, actual);
    const pass = matches.pass;
    if (pass) {
      // Snapshot matches with the story
      return {state: 'matched'};
    }

    // Snapshot does not match story
    if (update) {
      snapshot.add(key, actual);
      return {state: 'updated'};
    }

    const diffMessage = diff(
      matches.expected.trim(),
      matches.actual.trim(),
      {
        aAnnotation: 'Snapshot',
        bAnnotation: 'Current story',
      },
    );

    if (interactive) {
      const shouldUpdate = await this.confirmUpate(diffMessage);
      if (shouldUpdate) {
        snapshot.add(key, actual);
        return {state: 'updated'};
      }
    }

    return {state: 'unmatched', message: diffMessage};
  }

  endKind({update}) {
    const snapshot = this.state.snapshot;
    if (update) {
      snapshot.removeUncheckedKeys();
    }
    snapshot.save(update);
  }

  async confirmUpate(diffMessage) {
    process.stdout.write('\nReceived story is different from stored snapshot.\n');
    process.stdout.write('  ' + diffMessage.split('\n').join('\n  '));
    let ans = await promptly.prompt('Update snapshot? (y/n)');
    while (ans !== 'y' && ans !== 'n') {
      process.stdout.write('Enter only y (yes) or n (no)\n');
      ans = await promptly.prompt('Update snapshot? (y/n)');
    }
    process.stdout.write('\n');

    return ans === 'y';
  }
}
{'fraction_non_alphanumeric': 0.13969597844910525, 'fraction_numerical': 0.00038483740619588223, 'mean_word_length': 2.8024871982443305, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '1193005', 'n_tokens_mistral': 1588, 'n_tokens_neox': 1566, 'n_words': 400}