Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gyp: update xml string encoding conversion #1203

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions gyp/pylib/gyp/easy_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import re
import os
import locale


def XmlToString(content, encoding='utf-8', pretty=False):
Expand Down Expand Up @@ -115,11 +116,10 @@ def WriteXmlIfChanged(content, path, encoding='utf-8', pretty=False,
xml_string = XmlToString(content, encoding, pretty)
if win32 and os.linesep != '\r\n':
xml_string = xml_string.replace('\n', '\r\n')

try:
xml_string = xml_string.encode(encoding)
except Exception:
xml_string = unicode(xml_string, 'latin-1').encode(encoding)

default_encoding = locale.getdefaultlocale()[1]
if default_encoding.upper() != encoding.upper():
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should test default_encoding and default_encoding.upper() != encoding.upper():

xml_string = xml_string.decode(default_encoding).encode(encoding)

# Get the old content
try:
Expand Down
62 changes: 57 additions & 5 deletions test/test-addon.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
'use strict'

var test = require('tape')
var execFile = require('child_process').execFile
var path = require('path')
var fs = require('graceful-fs')
var child_process = require('child_process')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question to @nodejs/node-gyp
Can we use destructuring?

const { execFileSync, execFile } = require('child_process')

Or do we test on pre ES6 versions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the CI still tests 0.10 and 0.12.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not any more 😄

Copy link
Contributor Author

@lc-soft lc-soft Jun 8, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack
Do I need rename child_process to childProcess ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

var addonPath = path.resolve(__dirname, 'node_modules', 'hello_world')
var nodeGyp = path.resolve(__dirname, '..', 'bin', 'node-gyp.js')
var execFileSync = child_process.execFileSync
var execFile = child_process.execFile

function runHello() {
var testCode = "console.log(require('hello_world').hello())"
return execFileSync('node', ['-e', testCode], { cwd: __dirname }).toString()
}

test('build simple addon', function (t) {
t.plan(3)
Expand All @@ -16,12 +24,56 @@ test('build simple addon', function (t) {
var lastLine = logLines[logLines.length-1]
t.strictEqual(err, null)
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
t.strictEqual(runHello().trim(), 'world')
})
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
})

test('build simple addon in path with non-ascii characters', function (t) {
t.plan(3)
Copy link
Contributor Author

@lc-soft lc-soft May 24, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@refack OK, this test has been able to run successfully.

F:\repos\GitHub\node-gyp>node ./test/test-addon.js
TAP version 13
# build simple addon
ok 1 should be equal
ok 2 should end in ok
ok 3 should be equal
# build simple addon in path with non-ascii characters
ok 4 should be equal
ok 5 should end in ok
ok 6 should be equal

1..6
# tests 6
# pass  6

# ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍


var data, config, nodeDir, testNodeDir
var configPath = path.join(addonPath, 'build', 'config.gypi')

try {
data = fs.readFileSync(configPath, 'utf8')
} catch (err) {
t.error(err)
return
}
config = JSON.parse(data.replace(/\#.+\n/, ''))
nodeDir = config.variables.nodedir
// Create path with non-ascii characters
testNodeDir = path.join(addonPath, '非英文字符')
try {
fs.symlinkSync(nodeDir, testNodeDir, 'dir')
} catch (err) {
switch (err.code) {
case 'EEXIST': break
case 'EPERM':
t.error(err, 'Please try to running console as an administrator')
return
default:
t.error(err)
return
}
}

var cmd = [nodeGyp, 'rebuild', '-C', addonPath,
'--loglevel=verbose', '-nodedir=' + testNodeDir]
var proc = execFile(process.execPath, cmd, function (err, stdout, stderr) {
try {
var binding = require('hello_world')
t.strictEqual(binding.hello(), 'world')
} catch (error) {
t.error(error, 'load module')
fs.unlink(testNodeDir)
} catch (err) {
t.error(err)
}

var logLines = stderr.toString().trim().split(/\r?\n/)
var lastLine = logLines[logLines.length-1]
t.strictEqual(err, null)
t.strictEqual(lastLine, 'gyp info ok', 'should end in ok')
t.strictEqual(runHello().trim(), 'world')
})
proc.stdout.setEncoding('utf-8')
proc.stderr.setEncoding('utf-8')
Expand Down