produce.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /**
  2. * Created by Herby on 31. 5. 2014.
  3. */
  4. var builder = require('../lib/config-builder'),
  5. assert = require('assert'),
  6. path = require('path');
  7. function fixture(fixturePath) {
  8. return path.join(__dirname, fixturePath);
  9. }
  10. describe('#produceConfigObject merging', function () {
  11. it('should include local.amd.json if none other is present', function (done) {
  12. builder.produceConfigObject(fixture('single-local'), function (err, result) {
  13. assert.ifError(err);
  14. assert.deepEqual(result, {
  15. config: {foo: {foo: "bar"}}
  16. });
  17. done();
  18. });
  19. });
  20. it('should work root being \'.\'', function (done) {
  21. process.chdir(fixture('single-local'));
  22. builder.produceConfigObject('.', function (err, result) {
  23. assert.ifError(err);
  24. assert.deepEqual(result, {
  25. config: {foo: {foo: "bar"}}
  26. });
  27. done();
  28. });
  29. });
  30. it('should fail if foo.amd.json is present and there is no foo', function (done) {
  31. builder.produceConfigObject(fixture('local-and-missing'), function (err, result) {
  32. assert.ok(err);
  33. done();
  34. });
  35. });
  36. it('should fail if there is no applicable file', function (done) {
  37. builder.produceConfigObject(fixture('nothing'), function (err, result) {
  38. assert.ok(err);
  39. done();
  40. });
  41. });
  42. it('should include deep local.amd.json if none other is present', function (done) {
  43. builder.produceConfigObject(fixture('single-deep-local'), function (err, result) {
  44. assert.ifError(err);
  45. assert.deepEqual(result, {
  46. config: {fooDeep: {foo: "bar"}}
  47. });
  48. done();
  49. });
  50. });
  51. it('should include both local.amd.json if root and deep are present', function (done) {
  52. builder.produceConfigObject(fixture('two-locals'), function (err, result) {
  53. assert.ifError(err);
  54. assert.deepEqual(result, {
  55. config: {foo: {foo: "bar"}, fooDeep: {foo: "bar"}}
  56. });
  57. done();
  58. });
  59. });
  60. it('should include both local.amd.json if root and deep are present, parent should win', function (done) {
  61. builder.produceConfigObject(fixture('two-locals-with-shared'), function (err, result) {
  62. assert.ifError(err);
  63. assert.deepEqual(result, {
  64. config: {foo: {foo: "bar"}, fooDeep: {foo: "bar"}, shared: "root"}
  65. });
  66. done();
  67. });
  68. });
  69. it('should include both local.amd.json if root and deep are present, parent should be first in arrays', function (done) {
  70. builder.produceConfigObject(fixture('two-locals-with-shared-array'), function (err, result) {
  71. assert.ifError(err);
  72. assert.deepEqual(result, {
  73. config: {foo: {foo: "bar"}, fooDeep: {foo: "bar"}, shared: ["root", 0, "deep", 0]}
  74. });
  75. done();
  76. });
  77. });
  78. it('should include {root,deep}/local.amd.json and {foo,bar}.amd.json given {foo,bar} dir is present, locals should win', function (done) {
  79. builder.produceConfigObject(fixture('two-locals-two-others'), function (err, result) {
  80. assert.ifError(err);
  81. assert.deepEqual(result, {
  82. config: {a: 1, b: 1, c: 1, d: 1, e: 1, f: 1}
  83. });
  84. done();
  85. });
  86. });
  87. it('should include {root,deep}/local.amd.json and {foo,bar}.amd.json given {foo,bar} dir is present, locals and parents should win', function (done) {
  88. builder.produceConfigObject(fixture('two-locals-two-others-with-shared'), function (err, result) {
  89. assert.ifError(err);
  90. assert.deepEqual(result, {
  91. config: {a: 1, b: 1, c: 1, d: 1, e: 1, f: 1, g: 2, h: 1}
  92. });
  93. done();
  94. });
  95. });
  96. });
  97. describe('#produceConfigObject knows to deal with shims', function () {
  98. it('should output default array as {deps: anArray} in shim', function (done) {
  99. builder.produceConfigObject(fixture('single-local-shim'), function (err, result) {
  100. assert.ifError(err);
  101. assert.deepEqual(result, {
  102. shim: {foo: {deps: ["bar"]}}
  103. });
  104. done();
  105. });
  106. });
  107. it('should merge shims from {root,deep}/local.amd.json and from {foo,bar}.amd.json given {foo,bar} dir is present, locals and parent should win or be first', function (done) {
  108. builder.produceConfigObject(fixture('two-locals-two-others-shim'), function (err, result) {
  109. assert.ifError(err);
  110. assert.deepEqual(result, {
  111. config: {a: 1, b: 1, c: 1, d: 1, e: 1, f: 1},
  112. shim: {
  113. a: {deps: ["deeplocal", "other"], exports: "other"},
  114. b: {deps: ["rootlocal", "deep"], exports: "rootlocal"}
  115. }
  116. });
  117. done();
  118. });
  119. });
  120. });
  121. describe('#produceConfigObject paths', function () {
  122. it('should leave root local.amd.json paths unchanged', function (done) {
  123. builder.produceConfigObject(fixture('single-local-paths'), function (err, result) {
  124. assert.ifError(err);
  125. assert.deepEqual(result, {
  126. paths: {dot: ".", rel: "relative", sibling: "../sibling", abs: "/absolute", uri: "http://example.com/uri"}
  127. });
  128. done();
  129. });
  130. });
  131. it('should make deep local.amd.json paths relative to deep', function (done) {
  132. builder.produceConfigObject(fixture('single-deep-local-paths'), function (err, result) {
  133. assert.ifError(err);
  134. assert.deepEqual(result, {
  135. paths: {dot: "deep", rel: "deep/relative", sibling: "sibling", abs: "/absolute", uri: "http://example.com/uri"}
  136. });
  137. done();
  138. });
  139. });
  140. it('should make foo.amd.json paths relative to foo that is least deep', function (done) {
  141. builder.produceConfigObject(fixture('single-other-paths'), function (err, result) {
  142. assert.ifError(err);
  143. assert.deepEqual(result, {
  144. paths: {dot: "z2/other", rel: "z2/other/relative", sibling: "z2/sibling", abs: "/absolute", uri: "http://example.com/uri"}
  145. });
  146. done();
  147. });
  148. });
  149. it('should work with array paths correctly', function (done) {
  150. builder.produceConfigObject(fixture('arrays-in-paths'), function (err, result) {
  151. assert.ifError(err);
  152. assert.deepEqual(result, {
  153. paths: {
  154. dot0: ".", rel0: "relative", sibling0: "../sibling", abs0: "/absolute", uri0: "http://example.com/uri",
  155. arr0: [".", "relative", "../sibling", "/absolute", "http://example.com/uri"],
  156. dot1: "deep", rel1: "deep/relative", sibling1: "sibling", abs1: "/absolute", uri1: "http://example.com/uri",
  157. arr1: ["deep", "deep/relative", "sibling", "/absolute", "http://example.com/uri"],
  158. dot2: "z2/other", rel2: "z2/other/relative", sibling2: "z2/sibling", abs2: "/absolute", uri2: "http://example.com/uri",
  159. arr2: ["z2/other", "z2/other/relative", "z2/sibling", "/absolute", "http://example.com/uri"]
  160. }
  161. });
  162. done();
  163. });
  164. });
  165. it('should work with same paths correctly if used more times', function (done) {
  166. builder.produceConfigObject(fixture('arrays-in-paths'), function (err, result) {
  167. assert.ifError(err);
  168. assert.deepEqual(result, {
  169. paths: {
  170. dot0: ".", rel0: "relative", sibling0: "../sibling", abs0: "/absolute", uri0: "http://example.com/uri",
  171. arr0: [".", "relative", "../sibling", "/absolute", "http://example.com/uri"],
  172. dot1: "deep", rel1: "deep/relative", sibling1: "sibling", abs1: "/absolute", uri1: "http://example.com/uri",
  173. arr1: ["deep", "deep/relative", "sibling", "/absolute", "http://example.com/uri"],
  174. dot2: "z2/other", rel2: "z2/other/relative", sibling2: "z2/sibling", abs2: "/absolute", uri2: "http://example.com/uri",
  175. arr2: ["z2/other", "z2/other/relative", "z2/sibling", "/absolute", "http://example.com/uri"]
  176. }
  177. });
  178. done();
  179. });
  180. });
  181. });