You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. var assert = require('assert');
  2. var fs = require('fs');
  3. var sudo = require('./');
  4. var exec = require('child_process').exec;
  5. function kill(end) {
  6. if (process.platform === 'win32') return end();
  7. exec('sudo -k', end);
  8. }
  9. function icns() {
  10. if (process.platform !== 'darwin') return undefined;
  11. var path = '/Applications/Electron.app/Contents/Resources/Electron.icns';
  12. try {
  13. fs.statSync(path);
  14. return path;
  15. } catch (error) {}
  16. return undefined;
  17. }
  18. kill(
  19. function() {
  20. var options = {
  21. env: { 'SUDO_PROMPT_TEST_ENV': 'hello world' },
  22. icns: icns(),
  23. name: 'Electron'
  24. };
  25. if (process.platform === 'win32') {
  26. var command = 'echo %SUDO_PROMPT_TEST_ENV%';
  27. var expected = 'hello world\r\n';
  28. } else {
  29. // We use double quotes to tell echo to preserve internal space:
  30. var command = 'echo "$SUDO_PROMPT_TEST_ENV"';
  31. var expected = 'hello world\n';
  32. }
  33. console.log(
  34. 'sudo.exec(' +
  35. JSON.stringify(command) + ', ' +
  36. JSON.stringify(options) +
  37. ')'
  38. );
  39. sudo.exec(command, options,
  40. function(error, stdout, stderr) {
  41. console.log('error:', error);
  42. console.log('stdout: ' + JSON.stringify(stdout));
  43. console.log('stderr: ' + JSON.stringify(stderr));
  44. assert(error === undefined || typeof error === 'object');
  45. assert(stdout === undefined || typeof stdout === 'string');
  46. assert(stderr === undefined || typeof stderr === 'string');
  47. kill(
  48. function() {
  49. if (error) throw error;
  50. if (stdout !== expected) {
  51. throw new Error('stdout != ' + JSON.stringify(expected));
  52. }
  53. if (stderr !== '') {
  54. throw new Error('stderr != ""');
  55. }
  56. console.log('OK');
  57. }
  58. );
  59. }
  60. );
  61. }
  62. );