Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
konstantin committed Nov 13, 2015
1 parent fbe279b commit 00e0e9a
Show file tree
Hide file tree
Showing 12 changed files with 206 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,5 @@ build/Release
# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules

config.js

4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
sudo: false
language: node_js
node_js:
- "iojs"
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
[![Build Status](https://travis-ci.org/TargetProcess/tau-transifex.svg)](https://travis-ci.org/TargetProcess/tau-transifex)

Simple API for request to transifex REST API
6 changes: 6 additions & 0 deletions config.example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
projectSlug:'test',
resourceSlug:'test',
login: 'test',
password: 'test'
};
8 changes: 8 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
var config = require('./config');
var api = require('./index')(config);
api.updateResourceFile({
"none": {"deep nested message": "deep nested message", "test1": "test1"},
"custom_js_scope": {"custom js scope": "custom js scope"},
"remove": {"remove": "remove"}
});

56 changes: 27 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,21 @@ var request = require('request');
var _ = require('lodash');
var apiUrl = '/api/2/';
var host = 'https://transifex.com';
var Promise = require("bluebird");
var Promise = require('bluebird');
var utils = require('./lib/utils');
var generateHash = utils.generateHash;
var mergeStrings = utils.mergeStrings;
/**
*
* @param {{login:String, password:String, projectSlug: String, resourceSlug: String, skipTags: Array[String], requestConcurrency: Number, stringWillRemove:{tags:Array[String]}}} config
* @param {{login:String, password:String, projectSlug: String, resourceSlug: String, skipTags: Array[String], obsoleteTag:String, requestConcurrency: Number, stringWillRemove:{tags:Array[String]}}} config
* @return {{getTranslatedResources: Function, updateResourceFile: Function}}
*/
var transifex = function (config) {
var concurrency = config.requestConcurrency || 5;
config.stringWillRemove = config.stringWillRemove || {tags: []};
config.obsoleteTag = config.obsoleteTag || 'obsolete';
var resourceFile = `${config.projectSlug}/resource/${config.resourceSlug}/`;

var makeRequest = function (url, method, data) {
method = method || 'GET';
// console.log(method + ' ', apiUrl + url);
Expand Down Expand Up @@ -45,7 +52,6 @@ var transifex = function (config) {
return data.teams;
});
};
var resourceFile = `${config.projectSlug}/resource/${config.resourceSlug}/`;
var getTranslation = function (langCode) {
var url = `project/${resourceFile}translation/${langCode}/?mode=reviewed`;
return getResponse(url).then(function (data) {
Expand All @@ -59,15 +65,6 @@ var transifex = function (config) {
return Promise.all(resources)
})
};

var generateHash = function (key) {
var crypto = require('crypto');
var shasum = crypto.createHash('md5');
var escaped = key.replace(/\\/g, '\\\\').replace(/\./g, '\\.');
shasum.update(escaped + ":", 'utf8');
return shasum.digest('hex');
};

var getResourceStrings = function (strings) {
return Promise.map(_.toArray(strings), function (token) {
var url = `project/${resourceFile}source/${generateHash(token)}`;
Expand All @@ -77,7 +74,6 @@ var transifex = function (config) {
});
}, {concurrency: concurrency});
};

var putResourceStrings = function (strings) {
return Promise.map(strings, function (value) {
var url = `project/${resourceFile}source/${generateHash(value.token)}`;
Expand All @@ -86,6 +82,16 @@ var transifex = function (config) {
return strings;
});
};
var getLanguagesInfo = function () {
var url = `languages/`;
return getResponse(url).then(function (languages) {
return languages.map(function (lang) {
lang.code = lang.code.replace('_', '-');
return lang;
});
})
};

var removeStringsWithCertainTags = function (strings, tags) {
var content = _.reduce(strings, function (content, item) {
if (_.contains.apply(_, [item.tags || []].concat(tags))) {
Expand All @@ -97,17 +103,19 @@ var transifex = function (config) {
var url = `project/${resourceFile}content/`;
return makeRequest(url, 'PUT', {content: JSON.stringify(content)})
};


var updateResourceFile = function (dictionaries) {
var url = `project/${resourceFile}content/`;
return getResponse(url).then(function (res) {
var contentFromResource = JSON.parse(res.content);
return _.merge(contentFromResource, _.defaults.apply(_, [{}].concat(_.values(dictionaries))));
}).then(function (content) {
return Promise.all([makeRequest(url, 'PUT', {content: JSON.stringify(content)}), content]);
}).then(function (res) {
return getResourceStrings(res[1]);
return mergeStrings(dictionaries, contentFromResource);
}).then(function (strings) {
return _.map(_.compact(strings), function (string) {
return Promise.all([makeRequest(url, 'PUT', {content: JSON.stringify(strings.updateStrings)}), strings]);
}).then(function (res) {
return Promise.all([getResourceStrings(res[1].updateStrings), res[1].obsoleteStrings]);
}).then(function (res) {
return _.map(_.compact(res[0]), function (string) {
var token = string.token;
_.each(dictionaries, function (dictionary, scope) {
if (dictionary[token]) {
Expand All @@ -124,16 +132,6 @@ var transifex = function (config) {
});
};

var getLanguagesInfo = function () {
var url = `languages/`;
return getResponse(url).then(function (languages) {
return languages.map(function (lang) {
lang.code = lang.code.replace('_', '-');
return lang;
});
})
};

return {
getTranslatedResources: getTranslatedResources,
updateResourceFile: updateResourceFile,
Expand Down
46 changes: 46 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
var _ = require('lodash');
module.exports = {
mergeStrings: function (newStrings, fromTransifex) {
var flattenStrings = _.defaults.apply(_, [{}].concat(_.values(newStrings)));
var updateStrings = _.merge({}, fromTransifex, flattenStrings);
var obsoleteStrings = _.difference(_.values(fromTransifex), _.values(flattenStrings));
return {
updateStrings: updateStrings,
obsoleteStrings: obsoleteStrings
};
},
generateHash: function (key) {
var crypto = require('crypto');
var shasum = crypto.createHash('md5');
var escaped = key.replace(/\\/g, '\\\\').replace(/\./g, '\\.');
shasum.update(escaped + ":", 'utf8');
return shasum.digest('hex');
},
applyTagsToStrings: function (newStings, stringsWithTags, obsoleteStrings, config) {
var obsoleteTag = config.obsoleteTag;
var strings = _.map(_.compact(stringsWithTags), function (string) {
var token = string.token;
_.each(newStings, function (dictionary, scope) {
var tags = [];
if (dictionary[token]) {
tags = _.chain((string.tags || []).concat(scope)).compact().uniq().value();
tags = _.without(tags, obsoleteTag);
tags =_.uniq(tags);
string.tags = _.difference(tags, config.skipTags);
} else {
if(_.contains(obsoleteStrings, token)) {
tags = _.chain((string.tags || []).concat(scope)).compact().uniq().value();
tags.push(obsoleteTag);
tags =_.uniq(tags);
string.tags = _.difference(tags, config.skipTags);
}
}
});
return string;
});
return strings;
},
removeStringsWithCertainTags: function () {

}
};
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "Simple API for request to transifex REST API",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test/test.js"
},
"repository": {
"type": "git",
Expand All @@ -23,5 +23,8 @@
"bluebird": "^2.10.0",
"lodash": "^3.10.1",
"request": "^2.61.0"
},
"devDependencies": {
"tape": "^4.2.2"
}
}
5 changes: 5 additions & 0 deletions test/fextures/newStrings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = {
"none": {"deep nested message": "deep nested message", "test1": "test1"},
"custom_js_scope": {"custom js scope": "custom js scope"},
"remove": {"remove": "remove"}
};
4 changes: 4 additions & 0 deletions test/fextures/transifexStrings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
test: 'test',
test1: 'test1'
};
4 changes: 4 additions & 0 deletions test/fextures/transifexStrings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"test": "test",
"test1": "test1"
}
95 changes: 95 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
var utils = require('../lib/utils');
var test = require('tape');
var newStings = require('./fextures/newStrings');
var transifexStrings = require('./fextures/transifexStrings');
var stringsWithTags =
[
{
"comment": "",
"character_limit": null,
"tags": [
"none"
],
"token": "deep nested message"
},
{
"comment": "",
"character_limit": null,
"tags": [
"none"
],
"token": "test1"
},
{
"comment": "",
"character_limit": null,
"tags": null,
"token": "test"
},
{
"comment": "",
"character_limit": null,
"tags": [
"custom_js_scope"
],
"token": "custom js scope"
},
{
"comment": "",
"character_limit": null,
"tags": [
"remove"
],
"token": "remove"
}

];

test('generate hash', function (assert) {
assert.equal(
utils.generateHash('Possible transitions are: {currentStateName} → {nextStateNames}.'),
'43861bf525d30cbbce0c9d0950615645'
);
assert.equal(
utils.generateHash('Possible \\'),
'c3b342eb9097ddcb0f9d2ef0a312be0c'
);
assert.end();
});

test('merge strings', function (assert) {
assert.deepEqual(
utils.mergeStrings(newStings, transifexStrings),
{
obsoleteStrings: ['test'],
updateStrings: {
'custom js scope': 'custom js scope',
'deep nested message': 'deep nested message',
remove: 'remove',
test: 'test',
test1: 'test1'
}
}
);
assert.end();
});

test('apply tags', function (assert) {
assert.deepEqual(
utils.applyTagsToStrings(newStings, stringsWithTags, ['test'], {
obsoleteTag: 'obsolete',
skipTags: ['remove']
}),
{
obsoleteStrings: ['test'],
updateStrings: {
'custom js scope': 'custom js scope',
'deep nested message': 'deep nested message',
remove: 'remove',
test: 'test',
test1: 'test1'
}
}
);
assert.end();
});

0 comments on commit 00e0e9a

Please sign in to comment.