config-file-spec.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. const fs = require('fs-plus');
  2. const path = require('path');
  3. const temp = require('temp').track();
  4. const dedent = require('dedent');
  5. const ConfigFile = require('../src/config-file');
  6. describe('ConfigFile', () => {
  7. let filePath, configFile, subscription;
  8. beforeEach(async () => {
  9. jasmine.useRealClock();
  10. const tempDir = fs.realpathSync(temp.mkdirSync());
  11. filePath = path.join(tempDir, 'the-config.cson');
  12. });
  13. afterEach(() => {
  14. subscription.dispose();
  15. });
  16. describe('when the file does not exist', () => {
  17. it('returns an empty object from .get()', async () => {
  18. configFile = new ConfigFile(filePath);
  19. subscription = await configFile.watch();
  20. expect(configFile.get()).toEqual({});
  21. });
  22. });
  23. describe('when the file is empty', () => {
  24. it('returns an empty object from .get()', async () => {
  25. writeFileSync(filePath, '');
  26. configFile = new ConfigFile(filePath);
  27. subscription = await configFile.watch();
  28. expect(configFile.get()).toEqual({});
  29. });
  30. });
  31. describe('when the file is updated with valid CSON', () => {
  32. it('notifies onDidChange observers with the data', async () => {
  33. configFile = new ConfigFile(filePath);
  34. subscription = await configFile.watch();
  35. const event = new Promise(resolve => configFile.onDidChange(resolve));
  36. writeFileSync(
  37. filePath,
  38. dedent`
  39. '*':
  40. foo: 'bar'
  41. 'javascript':
  42. foo: 'baz'
  43. `
  44. );
  45. expect(await event).toEqual({
  46. '*': { foo: 'bar' },
  47. javascript: { foo: 'baz' }
  48. });
  49. expect(configFile.get()).toEqual({
  50. '*': { foo: 'bar' },
  51. javascript: { foo: 'baz' }
  52. });
  53. });
  54. });
  55. describe('when the file is updated with invalid CSON', () => {
  56. it('notifies onDidError observers', async () => {
  57. configFile = new ConfigFile(filePath);
  58. subscription = await configFile.watch();
  59. const message = new Promise(resolve => configFile.onDidError(resolve));
  60. writeFileSync(
  61. filePath,
  62. dedent`
  63. um what?
  64. `,
  65. 2
  66. );
  67. expect(await message).toContain('Failed to load `the-config.cson`');
  68. const event = new Promise(resolve => configFile.onDidChange(resolve));
  69. writeFileSync(
  70. filePath,
  71. dedent`
  72. '*':
  73. foo: 'bar'
  74. 'javascript':
  75. foo: 'baz'
  76. `,
  77. 4
  78. );
  79. expect(await event).toEqual({
  80. '*': { foo: 'bar' },
  81. javascript: { foo: 'baz' }
  82. });
  83. });
  84. });
  85. describe('ConfigFile.at()', () => {
  86. let path0, path1;
  87. beforeEach(() => {
  88. path0 = filePath;
  89. path1 = path.join(fs.realpathSync(temp.mkdirSync()), 'the-config.cson');
  90. configFile = ConfigFile.at(path0);
  91. });
  92. it('returns an existing ConfigFile', () => {
  93. const cf = ConfigFile.at(path0);
  94. expect(cf).toEqual(configFile);
  95. });
  96. it('creates a new ConfigFile for unrecognized paths', () => {
  97. const cf = ConfigFile.at(path1);
  98. expect(cf).not.toEqual(configFile);
  99. });
  100. });
  101. });
  102. function writeFileSync(filePath, content, seconds = 2) {
  103. const utime = Date.now() / 1000 + seconds;
  104. fs.writeFileSync(filePath, content);
  105. fs.utimesSync(filePath, utime, utime);
  106. }