test-environment.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. 'use strict';
  5. const { env } = require('sdk/system/environment');
  6. const { Cc, Ci } = require('chrome');
  7. const { get, set, exists } = Cc['@mozilla.org/process/environment;1'].
  8. getService(Ci.nsIEnvironment);
  9. exports['test exists'] = function(assert) {
  10. assert.equal('PATH' in env, exists('PATH'),
  11. 'PATH environment variable is defined');
  12. assert.equal('FOO1' in env, exists('FOO1'),
  13. 'FOO1 environment variable is not defined');
  14. set('FOO1', 'foo');
  15. assert.equal('FOO1' in env, true,
  16. 'FOO1 environment variable was set');
  17. set('FOO1', null);
  18. assert.equal('FOO1' in env, false,
  19. 'FOO1 environment variable was unset');
  20. };
  21. exports['test get'] = function(assert) {
  22. assert.equal(env.PATH, get('PATH'), 'PATH env variable matches');
  23. assert.equal(env.BAR2, undefined, 'BAR2 env variable is not defined');
  24. set('BAR2', 'bar');
  25. assert.equal(env.BAR2, 'bar', 'BAR2 env variable was set');
  26. set('BAR2', null);
  27. assert.equal(env.BAR2, undefined, 'BAR2 env variable was unset');
  28. };
  29. exports['test set'] = function(assert) {
  30. assert.equal(get('BAZ3'), '', 'BAZ3 env variable is not set');
  31. assert.equal(env.BAZ3, undefined, 'BAZ3 is not set');
  32. env.BAZ3 = 'baz';
  33. assert.equal(env.BAZ3, get('BAZ3'), 'BAZ3 env variable is set');
  34. assert.equal(get('BAZ3'), 'baz', 'BAZ3 env variable was set to "baz"');
  35. };
  36. exports['test unset'] = function(assert) {
  37. env.BLA4 = 'bla';
  38. assert.equal(env.BLA4, 'bla', 'BLA4 env variable is set');
  39. assert.equal(delete env.BLA4, true, 'BLA4 env variable is removed');
  40. assert.equal(env.BLA4, undefined, 'BLA4 env variable is unset');
  41. assert.equal('BLA4' in env, false, 'BLA4 env variable no longer exists' );
  42. };
  43. require('test').run(exports);