<commit_before>"use strict";

const assert = require("assert");
const path = require("path");
const shell = require("shelljs");
const spawn = require("cross-spawn");
const fs = require("fs-extra");
const os = require("os");
const xml2js = require("xml2js");
const temp = require("temp");
const byline = require("byline");
const stripAnsi = require("strip-ansi");

// Automatically track and cleanup files at exit
temp.track();

const elmTestPath = path.join(__dirname, "..", "bin", "elm-test");
const elmHome = path.join(__dirname, "..", "fixtures", "elm-home");
const spawnOpts = { silent: true, env: Object.assign({ELM_HOME: elmHome}, process.env)};

function elmTestWithYes(args, callback) {
  const child = spawn(elmTestPath, args, spawnOpts);

  child.stdin.setEncoding("utf-8");
  child.stdin.write(os.EOL);
  child.stdin.end();
  child.on('exit', (code) => {
    callback(code);
  });
}

function execElmTest(args) {
  return spawn.sync(elmTestPath, args, Object.assign({encoding: "utf-8"}, spawnOpts));
}

describe("flags", () => {
  describe("elm-test init", () => {
    beforeEach(() => {
      shell.pushd(temp.mkdirSync("elm-test-tests-"));
    });

    afterEach(() => {
      shell.popd();
    });

    describe("for a PACKAGE", () => {
      beforeEach(() => {
        shell.cp(path.join(__dirname, "templates", "package", "elm.json"), "elm.json");
      });

      it("Adds elm-explorations/test", (done) => {
        var json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
        assert.equal(typeof json["test-dependencies"]["elm-explorations/test"], "undefined");

        elmTestWithYes(["init"], (code) => {
          assert.equal(code, 0);

          json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
          assert.equal(typeof json["test-dependencies"]["elm-explorations/test"], "string");

          done();
        });
      });
    });

    describe("for an APPLICATION", () => {
      beforeEach(() => {
        shell.cp(path.join(__dirname, "templates", "application", "elm.json"), "elm.json");
      });

      it("Adds elm-explorations/test", (done) => {
        var json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
        assert.equal(typeof json["test-dependencies"]["direct"]["elm-explorations/test"], "undefined");

        elmTestWithYes(["init"], (code) => {
          assert.equal(code, 0);

          json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
          assert.equal(typeof json["test-dependencies"]["direct"]["elm-explorations/test"], "string");

          done();
        });
      });
    });
  });
  describe("elm-test install", () => {
    beforeEach(() => {
      shell.pushd(temp.mkdirSync("elm-test-tests-"));
    });

    afterEach(() => {
      shell.popd();
    });

    it("should fail if the current directory does not contain an elm.json", () => {
      shell.cp("-R", path.join(__dirname, "install", "*", "."));
      shell.rm("-f", "elm.json");

      const runResult = execElmTest(["install", "elm/regex"]);

      assert.notEqual(runResult.code, 0);
    });

    it.only("should not allow command injection", () => {
      shell.cp(path.join(__dirname, "templates", "application", "elm.json"), "elm.json");
      const runResult = spawn.sync(
        elmTestPath,
        ["install", "elm/regex; printf 'FINDME'; printf 'TWICE'"],
        Object.assign({ encoding: "utf-8", input: 'y\n' }, spawnOpts),
      );
      console.log(runResult);
      assert(!runResult.stdout.includes('FINDME'));
      assert(!runResult.stderr.includes('FINDMETWICE'));
    }).timeout(60000);
  });

  describe("--help", () => {
    it("Should print the usage", () => {
      const runResult = execElmTest(["--help"]);
      // Checking against a fixture is brittle here
      // For now, check that the output is non-empty.
      assert.ok(runResult.stdout.length > 0);
    });

    it("Should exit indicating failure", () => {
      const runResult = execElmTest(["--help"]);
      assert.notEqual(0, runResult.code);
    });
  });

  describe("--report", () => {
    it("Should be able to report json lines", () => {
      const runResult = execElmTest(["--report=json", "tests/OnePassing.elm"]);

      let linesReceived = 0;

      runResult.stdout.split("\n").forEach(line => {
        if (line.length === 0) {
          return;
        }

        linesReceived += 1;<commit_msg>Remove console.log<commit_after>"use strict";

const assert = require("assert");
const path = require("path");
const shell = require("shelljs");
const spawn = require("cross-spawn");
const fs = require("fs-extra");
const os = require("os");
const xml2js = require("xml2js");
const temp = require("temp");
const byline = require("byline");
const stripAnsi = require("strip-ansi");

// Automatically track and cleanup files at exit
temp.track();

const elmTestPath = path.join(__dirname, "..", "bin", "elm-test");
const elmHome = path.join(__dirname, "..", "fixtures", "elm-home");
const spawnOpts = { silent: true, env: Object.assign({ELM_HOME: elmHome}, process.env)};

function elmTestWithYes(args, callback) {
  const child = spawn(elmTestPath, args, spawnOpts);

  child.stdin.setEncoding("utf-8");
  child.stdin.write(os.EOL);
  child.stdin.end();
  child.on('exit', (code) => {
    callback(code);
  });
}

function execElmTest(args) {
  return spawn.sync(elmTestPath, args, Object.assign({encoding: "utf-8"}, spawnOpts));
}

describe("flags", () => {
  describe("elm-test init", () => {
    beforeEach(() => {
      shell.pushd(temp.mkdirSync("elm-test-tests-"));
    });

    afterEach(() => {
      shell.popd();
    });

    describe("for a PACKAGE", () => {
      beforeEach(() => {
        shell.cp(path.join(__dirname, "templates", "package", "elm.json"), "elm.json");
      });

      it("Adds elm-explorations/test", (done) => {
        var json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
        assert.equal(typeof json["test-dependencies"]["elm-explorations/test"], "undefined");

        elmTestWithYes(["init"], (code) => {
          assert.equal(code, 0);

          json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
          assert.equal(typeof json["test-dependencies"]["elm-explorations/test"], "string");

          done();
        });
      });
    });

    describe("for an APPLICATION", () => {
      beforeEach(() => {
        shell.cp(path.join(__dirname, "templates", "application", "elm.json"), "elm.json");
      });

      it("Adds elm-explorations/test", (done) => {
        var json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
        assert.equal(typeof json["test-dependencies"]["direct"]["elm-explorations/test"], "undefined");

        elmTestWithYes(["init"], (code) => {
          assert.equal(code, 0);

          json = JSON.parse(fs.readFileSync("elm.json", {encoding: "utf-8"}));
          assert.equal(typeof json["test-dependencies"]["direct"]["elm-explorations/test"], "string");

          done();
        });
      });
    });
  });
  describe("elm-test install", () => {
    beforeEach(() => {
      shell.pushd(temp.mkdirSync("elm-test-tests-"));
    });

    afterEach(() => {
      shell.popd();
    });

    it("should fail if the current directory does not contain an elm.json", () => {
      shell.cp("-R", path.join(__dirname, "install", "*", "."));
      shell.rm("-f", "elm.json");

      const runResult = execElmTest(["install", "elm/regex"]);

      assert.notEqual(runResult.code, 0);
    });

    it.only("should not allow command injection", () => {
      shell.cp(path.join(__dirname, "templates", "application", "elm.json"), "elm.json");
      const runResult = spawn.sync(
        elmTestPath,
        ["install", "elm/regex; printf 'FINDME'; printf 'TWICE'"],
        Object.assign({ encoding: "utf-8", input: 'y\n' }, spawnOpts),
      );
      assert(!runResult.stdout.includes('FINDME'));
      assert(!runResult.stderr.includes('FINDMETWICE'));
    }).timeout(60000);
  });

  describe("--help", () => {
    it("Should print the usage", () => {
      const runResult = execElmTest(["--help"]);
      // Checking against a fixture is brittle here
      // For now, check that the output is non-empty.
      assert.ok(runResult.stdout.length > 0);
    });

    it("Should exit indicating failure", () => {
      const runResult = execElmTest(["--help"]);
      assert.notEqual(0, runResult.code);
    });
  });

  describe("--report", () => {
    it("Should be able to report json lines", () => {
      const runResult = execElmTest(["--report=json", "tests/OnePassing.elm"]);

      let linesReceived = 0;

      runResult.stdout.split("\n").forEach(line => {
        if (line.length === 0) {
          return;
        }

        linesReceived += 1;
{'fraction_non_alphanumeric': 0.20509612103287453, 'fraction_numerical': 0.0050051188715732, 'mean_word_length': 3.087401208740121, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 53, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, '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': '4461854', 'n_tokens_mistral': 2839, 'n_tokens_neox': 2769, 'n_words': 529}