Skip to content

Commit d6139b5

Browse files
lc-softrefack
authored andcommitted
gyp: update xml string encoding conversion
* test: build simple addon in path with non-ascii characters * test: add test-charmap.py PR-URL: #1203 Reviewed-By: Refael Ackermann <[email protected]>
1 parent c84a541 commit d6139b5

File tree

3 files changed

+114
-10
lines changed

3 files changed

+114
-10
lines changed

gyp/pylib/gyp/easy_xml.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import re
66
import os
7+
import locale
78

89

910
def XmlToString(content, encoding='utf-8', pretty=False):
@@ -115,11 +116,10 @@ def WriteXmlIfChanged(content, path, encoding='utf-8', pretty=False,
115116
xml_string = XmlToString(content, encoding, pretty)
116117
if win32 and os.linesep != '\r\n':
117118
xml_string = xml_string.replace('\n', '\r\n')
118-
119-
try:
120-
xml_string = xml_string.encode(encoding)
121-
except Exception:
122-
xml_string = unicode(xml_string, 'latin-1').encode(encoding)
119+
120+
default_encoding = locale.getdefaultlocale()[1]
121+
if default_encoding.upper() != encoding.upper():
122+
xml_string = xml_string.decode(default_encoding).encode(encoding)
123123

124124
# Get the old content
125125
try:

test/fixtures/test-charmap.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import sys
2+
import locale
3+
4+
reload(sys)
5+
6+
def main():
7+
encoding = locale.getdefaultlocale()[1]
8+
sys.setdefaultencoding(encoding)
9+
textmap = {
10+
'cp936': u'\u4e2d\u6587',
11+
'cp1252': u'Lat\u012Bna',
12+
'cp932': u'\u306b\u307b\u3093\u3054'
13+
}
14+
if textmap.has_key(encoding):
15+
print textmap[encoding]
16+
return True
17+
18+
if __name__ == '__main__':
19+
print main()

test/test-addon.js

Lines changed: 90 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
'use strict'
22

33
var test = require('tape')
4-
var execFile = require('child_process').execFile
54
var path = require('path')
5+
var fs = require('graceful-fs')
6+
var child_process = require('child_process')
67
var addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
78
var nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
9+
var execFileSync = child_process.execFileSync
10+
var execFile = child_process.execFile
11+
12+
function runHello() {
13+
var testCode = "console.log(require('hello_world').hello())"
14+
return execFileSync('node', ['-e', testCode], { cwd: __dirname }).toString()
15+
}
16+
17+
function getEncoding() {
18+
var code = 'import locale;print locale.getdefaultlocale()[1]'
19+
return execFileSync('python', ['-c', code]).toString().trim()
20+
}
21+
22+
function checkCharmapValid() {
23+
var data
24+
try {
25+
data = execFileSync('python', ['fixtures/test-charmap.py'],
26+
{ cwd: __dirname })
27+
} catch (err) {
28+
return false
29+
}
30+
var lines = data.toString().trim().split('\n')
31+
return lines.pop() === 'True'
32+
}
833

934
test('build simple addon', function (t) {
1035
t.plan(3)
@@ -16,12 +41,72 @@ test('build simple addon', function (t) {
1641
var lastLine = logLines[logLines.length-1]
1742
t.strictEqual(err, null)
1843
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
44+
t.strictEqual(runHello().trim(), 'world')
45+
})
46+
proc.stdout.setEncoding('utf-8')
47+
proc.stderr.setEncoding('utf-8')
48+
})
49+
50+
test('build simple addon in path with non-ascii characters', function (t) {
51+
t.plan(1)
52+
53+
if (!checkCharmapValid()) {
54+
return t.skip('python console app can\'t encode non-ascii character.')
55+
}
56+
57+
var testDirNames = {
58+
'cp936': '文件夹',
59+
'cp1252': 'Latīna',
60+
'cp932': 'フォルダ'
61+
}
62+
// Select non-ascii characters by current encoding
63+
var testDirName = testDirNames[getEncoding()]
64+
// If encoding is UTF-8 or other then no need to test
65+
if (!testDirName) {
66+
return t.skip('no need to test')
67+
}
68+
69+
t.plan(3)
70+
71+
var data, configPath = path.join(addonPath, 'build', 'config.gypi')
72+
try {
73+
data = fs.readFileSync(configPath, 'utf8')
74+
} catch (err) {
75+
t.error(err)
76+
return
77+
}
78+
var config = JSON.parse(data.replace(/\#.+\n/, ''))
79+
var nodeDir = config.variables.nodedir
80+
var testNodeDir = path.join(addonPath, testDirName)
81+
// Create symbol link to path with non-ascii characters
82+
try {
83+
fs.symlinkSync(nodeDir, testNodeDir, 'dir')
84+
} catch (err) {
85+
switch (err.code) {
86+
case 'EEXIST': break
87+
case 'EPERM':
88+
t.error(err, 'Please try to running console as an administrator')
89+
return
90+
default:
91+
t.error(err)
92+
return
93+
}
94+
}
95+
96+
var cmd = [nodeGyp, 'rebuild', '-C', addonPath,
97+
'--loglevel=verbose', '-nodedir=' + testNodeDir]
98+
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
1999
try {
20-
var binding = require('hello_world')
21-
t.strictEqual(binding.hello(), 'world')
22-
} catch (error) {
23-
t.error(error, 'load module')
100+
fs.unlink(testNodeDir)
101+
} catch (err) {
102+
t.error(err)
24103
}
104+
105+
var logLines = stderr.toString().trim().split(/\r?\n/)
106+
var lastLine = logLines[logLines.length-1]
107+
t.strictEqual(err, null)
108+
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
109+
t.strictEqual(runHello().trim(), 'world')
25110
})
26111
proc.stdout.setEncoding('utf-8')
27112
proc.stderr.setEncoding('utf-8')

0 commit comments

Comments
 (0)