summaryrefslogtreecommitdiffstats
path: root/addon-sdk/source/bin/node-scripts/test.docs.js
blob: e6aef516d739c9e9dd59e0ac4f29ad4512a703b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* 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 createHash = require('crypto').createHash;
var fs = require("fs");
var fsExtra = require("fs-extra")
var path = require("path");
var Promise = require("promise");
var chai = require("chai");
var expect = chai.expect;
var teacher = require("teacher");

var rootURI = path.join(__dirname, "..", "..");

// get a list of words that fail spell check but are still acceptable
var NEW_WORDS = fs.readFileSync(path.join(__dirname, "words.txt")).toString().trim().split("\n");

var CACHE_PATH = path.join(__dirname, "..", "..", "cache", "spellchecks.json");

var CACHE = {};

try {
  CACHE = JSON.parse(fs.readFileSync(CACHE_PATH).toString());
}
catch (e) {}

function md5(str) {
  return createHash("md5").update(str).digest("utf8");
}

function addCacheHash(hash) {
  CACHE[hash] = true;
  fsExtra.ensureFileSync(CACHE_PATH);
  fsExtra.writeJSONSync(CACHE_PATH, CACHE);
}

describe("Spell Checking", function () {
  it("Spellcheck CONTRIBUTING.md", function (done) {
   var readme = path.join(rootURI, "CONTRIBUTING.md");

    fs.readFile(readme, function (err, data) {
      if (err) {
        throw err;
      }
      var text = data.toString();
      var hash = md5(text);

      // skip this test if we know we have done the
      // exact same test with positive results before
      if (CACHE[hash]) {
        expect(CACHE[hash]).to.be.equal(true);
        return done();
      }

      teacher.check(text, function(err, data) {
        expect(err).to.be.equal(null);

        var results = data || [];
        results = results.filter(function(result) {
          if (NEW_WORDS.indexOf(result.string.toLowerCase()) != -1) {
            return false;
          }

          // ignore anything that starts with a dash
          if (result.string[0] == "-") {
            return false;
          }

          if (!(new RegExp(result.string)).test(text)) {
            return false;
          }

          return true;
        })

        if (results.length > 0) {
          console.log(results);
        }
        else {
          addCacheHash(hash);
        }

        expect(results.length).to.be.equal(0);

        setTimeout(done, 500);
      });
    });
  });

  it("Spellcheck README.md", function (done) {
   var readme = path.join(rootURI, "README.md");

    fs.readFile(readme, function (err, data) {
      if (err) {
        throw err;
      }
      var text = data.toString();
      var hash = md5(text);

      // skip this test if we know we have done the
      // exact same test with positive results before
      if (CACHE[hash]) {
        expect(CACHE[hash]).to.be.equal(true);
        return done();
      }

      teacher.check(text, function(err, data) {
        expect(err).to.be.equal(null);

        var results = data || [];
        results = results.filter(function(result) {
          if (NEW_WORDS.indexOf(result.string.toLowerCase()) != -1) {
            return false;
          }

          // ignore anything that starts with a dash
          if (result.string[0] == "-") {
            return false;
          }

          // ignore anything that we don't find in the original text,
          // for some reason "bootstrap.js" becomes "bootstrapjs".
          if (!(new RegExp(result.string)).test(text)) {
            return false;
          }

          return true;
        })

        if (results.length > 0) {
          console.log(results);
        }
        else {
          addCacheHash(hash);
        }

        expect(results.length).to.be.equal(0);

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