decaffeinate: Convert CompileControllerTests.coffee and 17 other files to JS
This commit is contained in:
@@ -1,200 +1,262 @@
|
||||
SandboxedModule = require('sandboxed-module')
|
||||
sinon = require('sinon')
|
||||
require('chai').should()
|
||||
modulePath = require('path').join __dirname, '../../../app/js/UrlCache'
|
||||
EventEmitter = require("events").EventEmitter
|
||||
/*
|
||||
* decaffeinate suggestions:
|
||||
* DS101: Remove unnecessary use of Array.from
|
||||
* DS102: Remove unnecessary code created because of implicit returns
|
||||
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
||||
*/
|
||||
const SandboxedModule = require('sandboxed-module');
|
||||
const sinon = require('sinon');
|
||||
require('chai').should();
|
||||
const modulePath = require('path').join(__dirname, '../../../app/js/UrlCache');
|
||||
const { EventEmitter } = require("events");
|
||||
|
||||
describe "UrlCache", ->
|
||||
beforeEach ->
|
||||
@callback = sinon.stub()
|
||||
@url = "www.example.com/file"
|
||||
@project_id = "project-id-123"
|
||||
@UrlCache = SandboxedModule.require modulePath, requires:
|
||||
"./db" : {}
|
||||
"./UrlFetcher" : @UrlFetcher = {}
|
||||
"logger-sharelatex": @logger = {log: sinon.stub()}
|
||||
"settings-sharelatex": @Settings = { path: clsiCacheDir: "/cache/dir" }
|
||||
"fs": @fs = {}
|
||||
describe("UrlCache", function() {
|
||||
beforeEach(function() {
|
||||
this.callback = sinon.stub();
|
||||
this.url = "www.example.com/file";
|
||||
this.project_id = "project-id-123";
|
||||
return this.UrlCache = SandboxedModule.require(modulePath, { requires: {
|
||||
"./db" : {},
|
||||
"./UrlFetcher" : (this.UrlFetcher = {}),
|
||||
"logger-sharelatex": (this.logger = {log: sinon.stub()}),
|
||||
"settings-sharelatex": (this.Settings = { path: {clsiCacheDir: "/cache/dir"} }),
|
||||
"fs": (this.fs = {})
|
||||
}
|
||||
});});
|
||||
|
||||
describe "_doesUrlNeedDownloading", ->
|
||||
beforeEach ->
|
||||
@lastModified = new Date()
|
||||
@lastModifiedRoundedToSeconds = new Date(Math.floor(@lastModified.getTime() / 1000) * 1000)
|
||||
describe("_doesUrlNeedDownloading", function() {
|
||||
beforeEach(function() {
|
||||
this.lastModified = new Date();
|
||||
return this.lastModifiedRoundedToSeconds = new Date(Math.floor(this.lastModified.getTime() / 1000) * 1000);
|
||||
});
|
||||
|
||||
describe "when URL does not exist in cache", ->
|
||||
beforeEach ->
|
||||
@UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, null)
|
||||
@UrlCache._doesUrlNeedDownloading(@project_id, @url, @lastModified, @callback)
|
||||
describe("when URL does not exist in cache", function() {
|
||||
beforeEach(function() {
|
||||
this.UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, null);
|
||||
return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should return the callback with true", ->
|
||||
@callback.calledWith(null, true).should.equal true
|
||||
return it("should return the callback with true", function() {
|
||||
return this.callback.calledWith(null, true).should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "when URL does exist in cache", ->
|
||||
beforeEach ->
|
||||
@urlDetails = {}
|
||||
@UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, @urlDetails)
|
||||
return describe("when URL does exist in cache", function() {
|
||||
beforeEach(function() {
|
||||
this.urlDetails = {};
|
||||
return this.UrlCache._findUrlDetails = sinon.stub().callsArgWith(2, null, this.urlDetails);
|
||||
});
|
||||
|
||||
describe "when the modified date is more recent than the cached modified date", ->
|
||||
beforeEach ->
|
||||
@urlDetails.lastModified = new Date(@lastModified.getTime() - 1000)
|
||||
@UrlCache._doesUrlNeedDownloading(@project_id, @url, @lastModified, @callback)
|
||||
describe("when the modified date is more recent than the cached modified date", function() {
|
||||
beforeEach(function() {
|
||||
this.urlDetails.lastModified = new Date(this.lastModified.getTime() - 1000);
|
||||
return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should get the url details", ->
|
||||
@UrlCache._findUrlDetails
|
||||
.calledWith(@project_id, @url)
|
||||
.should.equal true
|
||||
it("should get the url details", function() {
|
||||
return this.UrlCache._findUrlDetails
|
||||
.calledWith(this.project_id, this.url)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should return the callback with true", ->
|
||||
@callback.calledWith(null, true).should.equal true
|
||||
return it("should return the callback with true", function() {
|
||||
return this.callback.calledWith(null, true).should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "when the cached modified date is more recent than the modified date", ->
|
||||
beforeEach ->
|
||||
@urlDetails.lastModified = new Date(@lastModified.getTime() + 1000)
|
||||
@UrlCache._doesUrlNeedDownloading(@project_id, @url, @lastModified, @callback)
|
||||
describe("when the cached modified date is more recent than the modified date", function() {
|
||||
beforeEach(function() {
|
||||
this.urlDetails.lastModified = new Date(this.lastModified.getTime() + 1000);
|
||||
return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should return the callback with false", ->
|
||||
@callback.calledWith(null, false).should.equal true
|
||||
return it("should return the callback with false", function() {
|
||||
return this.callback.calledWith(null, false).should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "when the cached modified date is equal to the modified date", ->
|
||||
beforeEach ->
|
||||
@urlDetails.lastModified = @lastModified
|
||||
@UrlCache._doesUrlNeedDownloading(@project_id, @url, @lastModified, @callback)
|
||||
describe("when the cached modified date is equal to the modified date", function() {
|
||||
beforeEach(function() {
|
||||
this.urlDetails.lastModified = this.lastModified;
|
||||
return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should return the callback with false", ->
|
||||
@callback.calledWith(null, false).should.equal true
|
||||
return it("should return the callback with false", function() {
|
||||
return this.callback.calledWith(null, false).should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "when the provided modified date does not exist", ->
|
||||
beforeEach ->
|
||||
@lastModified = null
|
||||
@UrlCache._doesUrlNeedDownloading(@project_id, @url, @lastModified, @callback)
|
||||
describe("when the provided modified date does not exist", function() {
|
||||
beforeEach(function() {
|
||||
this.lastModified = null;
|
||||
return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should return the callback with true", ->
|
||||
@callback.calledWith(null, true).should.equal true
|
||||
return it("should return the callback with true", function() {
|
||||
return this.callback.calledWith(null, true).should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "when the URL does not have a modified date", ->
|
||||
beforeEach ->
|
||||
@urlDetails.lastModified = null
|
||||
@UrlCache._doesUrlNeedDownloading(@project_id, @url, @lastModified, @callback)
|
||||
return describe("when the URL does not have a modified date", function() {
|
||||
beforeEach(function() {
|
||||
this.urlDetails.lastModified = null;
|
||||
return this.UrlCache._doesUrlNeedDownloading(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should return the callback with true", ->
|
||||
@callback.calledWith(null, true).should.equal true
|
||||
return it("should return the callback with true", function() {
|
||||
return this.callback.calledWith(null, true).should.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe "_ensureUrlIsInCache", ->
|
||||
beforeEach ->
|
||||
@UrlFetcher.pipeUrlToFile = sinon.stub().callsArg(2)
|
||||
@UrlCache._updateOrCreateUrlDetails = sinon.stub().callsArg(3)
|
||||
describe("_ensureUrlIsInCache", function() {
|
||||
beforeEach(function() {
|
||||
this.UrlFetcher.pipeUrlToFile = sinon.stub().callsArg(2);
|
||||
return this.UrlCache._updateOrCreateUrlDetails = sinon.stub().callsArg(3);
|
||||
});
|
||||
|
||||
describe "when the URL needs updating", ->
|
||||
beforeEach ->
|
||||
@UrlCache._doesUrlNeedDownloading = sinon.stub().callsArgWith(3, null, true)
|
||||
@UrlCache._ensureUrlIsInCache(@project_id, @url, @lastModified, @callback)
|
||||
describe("when the URL needs updating", function() {
|
||||
beforeEach(function() {
|
||||
this.UrlCache._doesUrlNeedDownloading = sinon.stub().callsArgWith(3, null, true);
|
||||
return this.UrlCache._ensureUrlIsInCache(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should check that the url needs downloading", ->
|
||||
@UrlCache._doesUrlNeedDownloading
|
||||
.calledWith(@project_id, @url, @lastModifiedRoundedToSeconds)
|
||||
.should.equal true
|
||||
it("should check that the url needs downloading", function() {
|
||||
return this.UrlCache._doesUrlNeedDownloading
|
||||
.calledWith(this.project_id, this.url, this.lastModifiedRoundedToSeconds)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should download the URL to the cache file", ->
|
||||
@UrlFetcher.pipeUrlToFile
|
||||
.calledWith(@url, @UrlCache._cacheFilePathForUrl(@project_id, @url))
|
||||
.should.equal true
|
||||
it("should download the URL to the cache file", function() {
|
||||
return this.UrlFetcher.pipeUrlToFile
|
||||
.calledWith(this.url, this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
|
||||
it "should update the database entry", ->
|
||||
@UrlCache._updateOrCreateUrlDetails
|
||||
.calledWith(@project_id, @url, @lastModifiedRoundedToSeconds)
|
||||
.should.equal true
|
||||
it("should update the database entry", function() {
|
||||
return this.UrlCache._updateOrCreateUrlDetails
|
||||
.calledWith(this.project_id, this.url, this.lastModifiedRoundedToSeconds)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should return the callback with the cache file path", ->
|
||||
@callback
|
||||
.calledWith(null, @UrlCache._cacheFilePathForUrl(@project_id, @url))
|
||||
.should.equal true
|
||||
return it("should return the callback with the cache file path", function() {
|
||||
return this.callback
|
||||
.calledWith(null, this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
|
||||
.should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "when the URL does not need updating", ->
|
||||
beforeEach ->
|
||||
@UrlCache._doesUrlNeedDownloading = sinon.stub().callsArgWith(3, null, false)
|
||||
@UrlCache._ensureUrlIsInCache(@project_id, @url, @lastModified, @callback)
|
||||
return describe("when the URL does not need updating", function() {
|
||||
beforeEach(function() {
|
||||
this.UrlCache._doesUrlNeedDownloading = sinon.stub().callsArgWith(3, null, false);
|
||||
return this.UrlCache._ensureUrlIsInCache(this.project_id, this.url, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should not download the URL to the cache file", ->
|
||||
@UrlFetcher.pipeUrlToFile
|
||||
.called.should.equal false
|
||||
it("should not download the URL to the cache file", function() {
|
||||
return this.UrlFetcher.pipeUrlToFile
|
||||
.called.should.equal(false);
|
||||
});
|
||||
|
||||
it "should return the callback with the cache file path", ->
|
||||
@callback
|
||||
.calledWith(null, @UrlCache._cacheFilePathForUrl(@project_id, @url))
|
||||
.should.equal true
|
||||
return it("should return the callback with the cache file path", function() {
|
||||
return this.callback
|
||||
.calledWith(null, this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
|
||||
.should.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe "downloadUrlToFile", ->
|
||||
beforeEach ->
|
||||
@cachePath = "path/to/cached/url"
|
||||
@destPath = "path/to/destination"
|
||||
@UrlCache._copyFile = sinon.stub().callsArg(2)
|
||||
@UrlCache._ensureUrlIsInCache = sinon.stub().callsArgWith(3, null, @cachePath)
|
||||
@UrlCache.downloadUrlToFile(@project_id, @url, @destPath, @lastModified, @callback)
|
||||
describe("downloadUrlToFile", function() {
|
||||
beforeEach(function() {
|
||||
this.cachePath = "path/to/cached/url";
|
||||
this.destPath = "path/to/destination";
|
||||
this.UrlCache._copyFile = sinon.stub().callsArg(2);
|
||||
this.UrlCache._ensureUrlIsInCache = sinon.stub().callsArgWith(3, null, this.cachePath);
|
||||
return this.UrlCache.downloadUrlToFile(this.project_id, this.url, this.destPath, this.lastModified, this.callback);
|
||||
});
|
||||
|
||||
it "should ensure the URL is downloaded and updated in the cache", ->
|
||||
@UrlCache._ensureUrlIsInCache
|
||||
.calledWith(@project_id, @url, @lastModified)
|
||||
.should.equal true
|
||||
it("should ensure the URL is downloaded and updated in the cache", function() {
|
||||
return this.UrlCache._ensureUrlIsInCache
|
||||
.calledWith(this.project_id, this.url, this.lastModified)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should copy the file to the new location", ->
|
||||
@UrlCache._copyFile
|
||||
.calledWith(@cachePath, @destPath)
|
||||
.should.equal true
|
||||
it("should copy the file to the new location", function() {
|
||||
return this.UrlCache._copyFile
|
||||
.calledWith(this.cachePath, this.destPath)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
return it("should call the callback", function() {
|
||||
return this.callback.called.should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "_deleteUrlCacheFromDisk", ->
|
||||
beforeEach ->
|
||||
@fs.unlink = sinon.stub().callsArg(1)
|
||||
@UrlCache._deleteUrlCacheFromDisk(@project_id, @url, @callback)
|
||||
describe("_deleteUrlCacheFromDisk", function() {
|
||||
beforeEach(function() {
|
||||
this.fs.unlink = sinon.stub().callsArg(1);
|
||||
return this.UrlCache._deleteUrlCacheFromDisk(this.project_id, this.url, this.callback);
|
||||
});
|
||||
|
||||
it "should delete the cache file", ->
|
||||
@fs.unlink
|
||||
.calledWith(@UrlCache._cacheFilePathForUrl(@project_id, @url))
|
||||
.should.equal true
|
||||
it("should delete the cache file", function() {
|
||||
return this.fs.unlink
|
||||
.calledWith(this.UrlCache._cacheFilePathForUrl(this.project_id, this.url))
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
return it("should call the callback", function() {
|
||||
return this.callback.called.should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "_clearUrlFromCache", ->
|
||||
beforeEach ->
|
||||
@UrlCache._deleteUrlCacheFromDisk = sinon.stub().callsArg(2)
|
||||
@UrlCache._clearUrlDetails = sinon.stub().callsArg(2)
|
||||
@UrlCache._clearUrlFromCache @project_id, @url, @callback
|
||||
describe("_clearUrlFromCache", function() {
|
||||
beforeEach(function() {
|
||||
this.UrlCache._deleteUrlCacheFromDisk = sinon.stub().callsArg(2);
|
||||
this.UrlCache._clearUrlDetails = sinon.stub().callsArg(2);
|
||||
return this.UrlCache._clearUrlFromCache(this.project_id, this.url, this.callback);
|
||||
});
|
||||
|
||||
it "should delete the file on disk", ->
|
||||
@UrlCache._deleteUrlCacheFromDisk
|
||||
.calledWith(@project_id, @url)
|
||||
.should.equal true
|
||||
it("should delete the file on disk", function() {
|
||||
return this.UrlCache._deleteUrlCacheFromDisk
|
||||
.calledWith(this.project_id, this.url)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should clear the entry in the database", ->
|
||||
@UrlCache._clearUrlDetails
|
||||
.calledWith(@project_id, @url)
|
||||
.should.equal true
|
||||
it("should clear the entry in the database", function() {
|
||||
return this.UrlCache._clearUrlDetails
|
||||
.calledWith(this.project_id, this.url)
|
||||
.should.equal(true);
|
||||
});
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
return it("should call the callback", function() {
|
||||
return this.callback.called.should.equal(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe "clearProject", ->
|
||||
beforeEach ->
|
||||
@urls = [
|
||||
"www.example.com/file1"
|
||||
return describe("clearProject", function() {
|
||||
beforeEach(function() {
|
||||
this.urls = [
|
||||
"www.example.com/file1",
|
||||
"www.example.com/file2"
|
||||
]
|
||||
@UrlCache._findAllUrlsInProject = sinon.stub().callsArgWith(1, null, @urls)
|
||||
@UrlCache._clearUrlFromCache = sinon.stub().callsArg(2)
|
||||
@UrlCache.clearProject @project_id, @callback
|
||||
];
|
||||
this.UrlCache._findAllUrlsInProject = sinon.stub().callsArgWith(1, null, this.urls);
|
||||
this.UrlCache._clearUrlFromCache = sinon.stub().callsArg(2);
|
||||
return this.UrlCache.clearProject(this.project_id, this.callback);
|
||||
});
|
||||
|
||||
it "should clear the cache for each url in the project", ->
|
||||
for url in @urls
|
||||
@UrlCache._clearUrlFromCache
|
||||
.calledWith(@project_id, url)
|
||||
.should.equal true
|
||||
it("should clear the cache for each url in the project", function() {
|
||||
return Array.from(this.urls).map((url) =>
|
||||
this.UrlCache._clearUrlFromCache
|
||||
.calledWith(this.project_id, url)
|
||||
.should.equal(true));
|
||||
});
|
||||
|
||||
it "should call the callback", ->
|
||||
@callback.called.should.equal true
|
||||
return it("should call the callback", function() {
|
||||
return this.callback.called.should.equal(true);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user