<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

var fs = require("fs-extra");
var path = require("path");
var utils = require("../utils");
var settings = require("../../lib/settings");
var chai = require("chai");
var expect = chai.expect;
var exec = utils.exec;

describe("jpm init", function () {
  beforeEach(utils.setup);
  afterEach(utils.tearDown);

  it("creates package.json with defaults", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();

    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var manifest = JSON.parse(fs.readFileSync(path.join(utils.tmpOutputDir, "package.json"), "utf-8"));
      expect(manifest.title).to.be.equal("My Jetpack Addon");
      expect(manifest.name).to.be.equal("tmp");
      expect(manifest.version).to.be.equal("0.0.1");
      expect(manifest.description).to.be.equal("A basic add-on");
      expect(manifest.main).to.be.equal("index.js");
      expect(manifest.author).to.be.equal("");
      expect(manifest.engines.firefox).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.engines.fennec).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.license).to.be.equal("MIT");
      done();
    });
  });

  it("creates package.json with custom entries", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = [
      'My Custom Name',
      'my-addon',
      '1.0.0',
      'A description',
      'lib/index.js',
      '<NAME>',
      'custom-firefox, nightly-firefox',
      'BSD',
      'yes'
    ];

    var proc = respond(exec("init"), responses.map(function (val) {
      return val + '\n';
    }));
    proc.on("close", function () {
      var manifest = JSON.parse(fs.readFileSync(path.join(utils.tmpOutputDir, "package.json"), "utf-8"));
      expect(manifest.title).to.be.equal(responses[0]);
      expect(manifest.name).to.be.equal(responses[1]);
      expect(manifest.version).to.be.equal(responses[2]);
      expect(manifest.description).to.be.equal(responses[3]);
      expect(manifest.main).to.be.equal(responses[4]);
      expect(manifest.author).to.be.equal(responses[5]);
      expect(manifest.engines['custom-firefox']).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.engines['nightly-firefox']).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.license).to.be.equal(responses[7]);
      done();
    });
  });

  it("sanitizes entries", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();
    responses[1] = "  an invalid $ _ name 123\n";
    responses[2] = "invalid version\n";
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var manifest = JSON.parse(fs.readFileSync(path.join(utils.tmpOutputDir, "package.json"), "utf-8"));
      expect(manifest.name).to.be.equal("aninvalidname123");
      expect(manifest.version).to.be.equal("0.0.0");
      done();
    });
  });

  it("copies in default index.js if it DNE", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "index.js"), "utf-8");
      var sourceIndex = fs.readFileSync(path.join("..", "..", "data", "index.js"), "utf-8");
      expect(dirIndex).to.be.equal(sourceIndex);
      done();
    });
  });

  it("does not copy in default index.js if it exists", function (done) {
    process.chdir(utils.tmpOutputDir);
    fs.writeFileSync(path.join(utils.tmpOutputDir, "index.js"), "hello");
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "index.js"), "utf-8");
      expect(dirIndex).to.be.equal("hello");
      done();
    });
  });

  it("copies in default README.md if it DNE", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "README.md"), "utf-8");
      var sourceIndex = fs.readFileSync(path.join("..", "..", "data", "README.md"), "utf-8");
      expect(dirIndex).to.be.equal(sourceIndex);
      done();
    });
  });

  it("does not copy in default README.md if it exists", function (done) {
    process.chdir(utils.tmpOutputDir);
    fs.writeFileSync(path.join(utils.tmpOutputDir, "README.md"), "readme!");
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "README.md"), "utf-8");
      expect(dirIndex).to.be.equal("readme!");<commit_msg>jpm init, test for default readme contents<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
"use strict";

var fs = require("fs-extra");
var path = require("path");
var os = require("os");
var utils = require("../utils");
var settings = require("../../lib/settings");
var chai = require("chai");
var expect = chai.expect;
var exec = utils.exec;

describe("jpm init", function () {
  beforeEach(utils.setup);
  afterEach(utils.tearDown);

  it("creates package.json with defaults", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();

    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var manifest = JSON.parse(fs.readFileSync(path.join(utils.tmpOutputDir, "package.json"), "utf-8"));
      expect(manifest.title).to.be.equal("My Jetpack Addon");
      expect(manifest.name).to.be.equal("tmp");
      expect(manifest.version).to.be.equal("0.0.1");
      expect(manifest.description).to.be.equal("A basic add-on");
      expect(manifest.main).to.be.equal("index.js");
      expect(manifest.author).to.be.equal("");
      expect(manifest.engines.firefox).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.engines.fennec).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.license).to.be.equal("MIT");
      done();
    });
  });

  it("creates package.json with custom entries", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = [
      'My Custom Name',
      'my-addon',
      '1.0.0',
      'A description',
      'lib/index.js',
      '<NAME>',
      'custom-firefox, nightly-firefox',
      'BSD',
      'yes'
    ];

    var proc = respond(exec("init"), responses.map(function (val) {
      return val + '\n';
    }));
    proc.on("close", function () {
      var manifest = JSON.parse(fs.readFileSync(path.join(utils.tmpOutputDir, "package.json"), "utf-8"));
      expect(manifest.title).to.be.equal(responses[0]);
      expect(manifest.name).to.be.equal(responses[1]);
      expect(manifest.version).to.be.equal(responses[2]);
      expect(manifest.description).to.be.equal(responses[3]);
      expect(manifest.main).to.be.equal(responses[4]);
      expect(manifest.author).to.be.equal(responses[5]);
      expect(manifest.engines['custom-firefox']).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.engines['nightly-firefox']).to.be.equal(
        ">=" + settings.MIN_VERSION);
      expect(manifest.license).to.be.equal(responses[7]);
      done();
    });
  });

  it("sanitizes entries", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();
    responses[1] = "  an invalid $ _ name 123\n";
    responses[2] = "invalid version\n";
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var manifest = JSON.parse(fs.readFileSync(path.join(utils.tmpOutputDir, "package.json"), "utf-8"));
      expect(manifest.name).to.be.equal("aninvalidname123");
      expect(manifest.version).to.be.equal("0.0.0");
      done();
    });
  });

  it("copies in default index.js if it DNE", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "index.js"), "utf-8");
      var sourceIndex = fs.readFileSync(path.join("..", "..", "data", "index.js"), "utf-8");
      expect(dirIndex).to.be.equal(sourceIndex);
      done();
    });
  });

  it("does not copy in default index.js if it exists", function (done) {
    process.chdir(utils.tmpOutputDir);
    fs.writeFileSync(path.join(utils.tmpOutputDir, "index.js"), "hello");
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "index.js"), "utf-8");
      expect(dirIndex).to.be.equal("hello");
      done();
    });
  });

  it("copies in default README.md if it DNE", function (done) {
    process.chdir(utils.tmpOutputDir);
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "README.md"), "utf-8");
      var defaultContents = "#My Jetpack Addon" + os.EOL + "A basic add-on";
      expect(dirIndex).to.be.equal(defaultContents);
      done();
    });
  });

  it("does not copy in default README.md if it exists", function (done) {
    process.chdir(utils.tmpOutputDir);
    fs.writeFileSync(path.join(utils.tmpOutputDir, "README.md"), "readme!");
    var responses = generateResponses();
    var proc = respond(exec("init"), responses);
    proc.on("close", function () {
      var dirIndex = fs.readFileSync(path.join(utils.tmpOutputDir, "README.md"), "utf-8");
      expect(dirIndex).to.be.equal("readme!");
{'fraction_non_alphanumeric': 0.18342734876483174, 'fraction_numerical': 0.007099786033845555, 'mean_word_length': 3.4304179233089185, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, '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': '96226', 'n_tokens_mistral': 3395, 'n_tokens_neox': 3316, 'n_words': 621}