helpers.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. var should = require("should")
  2. var helpers = require("../lib/helpers")
  3. var fse = require("fs-extra")
  4. var path = require("path")
  5. describe("helpers", function(){
  6. describe("willCollide(projectPath, outputPath)", function(){
  7. it("should collide if output path is /", function(done){
  8. helpers.willCollide("/", "/").should.be.true
  9. helpers.willCollide("/foo/bar/myproject", "/").should.be.true
  10. helpers.willCollide("/foo/bar/myproject/", "/").should.be.true
  11. done()
  12. })
  13. it("should collide if output path is the root of the source directory", function(done) {
  14. helpers.willCollide("/foo/bar/myproject", "/foo/bar/myproject").should.be.true
  15. helpers.willCollide("./", "./").should.be.true
  16. done()
  17. })
  18. it("should not collide if output path is /output", function(done){
  19. helpers.willCollide("/foo/bar/myproject", "/output").should.be.false
  20. helpers.willCollide("/foo/bar/myproject", "/output/").should.be.false
  21. helpers.willCollide("/foo/bar/myproject/", "/output/").should.be.false
  22. helpers.willCollide("/foo/bar/myproject/", "/output").should.be.false
  23. done()
  24. })
  25. it("should not collide if output path is in projectPath", function(done){
  26. helpers.willCollide("/foo/bar/myproject", "/foo/bar/myproject/www").should.be.false
  27. helpers.willCollide("/foo/bar/myproject/", "/foo/bar/myproject/www").should.be.false
  28. helpers.willCollide("/foo/bar/myproject", "/foo/bar/myproject/www/").should.be.false
  29. helpers.willCollide("/foo/bar/myproject/", "/foo/bar/myproject/www/").should.be.false
  30. done()
  31. })
  32. it("should not collide if project path is one back and begins with underscore", function(done){
  33. helpers.willCollide("/foo/bar/myproject", "/foo/bar").should.be.true
  34. helpers.willCollide("/foo/bar/myproject/", "/foo/bar/").should.be.true
  35. helpers.willCollide("/foo/bar/myproject", "/foo/bar/").should.be.true
  36. helpers.willCollide("/foo/bar/myproject/", "/foo/bar").should.be.true
  37. done()
  38. })
  39. })
  40. describe("willAllow(projectPath, outputPath)", function(){
  41. it("should not allow project to compile one directory back if source not starting with underscore", function(done){
  42. helpers.willAllow("/foo/bar/myproject", "/foo/bar").should.be.false
  43. helpers.willAllow("/foo/bar/myproject/", "/foo/bar/").should.be.false
  44. helpers.willAllow("/foo/bar/myproject", "/foo/bar/").should.be.false
  45. helpers.willAllow("/foo/bar/myproject/", "/foo/bar").should.be.false
  46. done()
  47. })
  48. it("should not allow project to compile into the source directory when no name is specified", function(done) {
  49. helpers.willAllow("/foo/bar/myproject", "/foo/bar/myproject").should.be.false
  50. helpers.willAllow("./", "./").should.be.false
  51. done()
  52. })
  53. it("should allow project to compile one directory back if source directory starts with underscore", function(done){
  54. helpers.willAllow("/foo/bar/_myproject", "/foo/bar").should.be.true
  55. helpers.willAllow("/foo/bar/_myproject/", "/foo/bar/").should.be.true
  56. helpers.willAllow("/foo/bar/_myproject", "/foo/bar/").should.be.true
  57. helpers.willAllow("/foo/bar/_myproject/", "/foo/bar").should.be.true
  58. done()
  59. })
  60. it("should not allow project to compile one directory back if source directory starts with underscore", function(done){
  61. helpers.willAllow("/foo/bar/_myproject", "/foo").should.be.false
  62. helpers.willAllow("/foo/_bar/myproject", "/foo").should.be.false
  63. helpers.willAllow("/foo/_bar/_myproject", "/foo").should.be.false
  64. done()
  65. })
  66. })
  67. describe("setup(projectPath)", function(){
  68. it("should detect framework style", function(done){
  69. var cfg = helpers.setup(path.join(__dirname, "apps", "app-style-framework"))
  70. cfg.should.have.property("config")
  71. cfg.should.have.property("projectPath")
  72. cfg.should.have.property("publicPath")
  73. done()
  74. })
  75. it("should detect root style", function(done){
  76. var cfg = helpers.setup(path.join(__dirname, "apps", "app-style-root"))
  77. cfg.should.have.property("config")
  78. cfg.should.have.property("projectPath")
  79. cfg.should.have.property("publicPath")
  80. cfg.publicPath.should.eql(cfg.projectPath)
  81. done()
  82. })
  83. it("should default to root style", function(done){
  84. var cfg = helpers.setup(path.join(__dirname, "apps", "app-style-implicit"))
  85. cfg.should.have.property("config")
  86. cfg.should.have.property("projectPath")
  87. cfg.should.have.property("publicPath")
  88. cfg.publicPath.should.eql(cfg.projectPath)
  89. done()
  90. })
  91. it("should replace values like $foo with process.env.foo", function(done){
  92. process.env.HARP_BASIC_AUTH = "jabberwocky:skrillex"
  93. var cfg = helpers.setup(path.join(__dirname, "apps", "envy"))
  94. cfg.should.have.property("config")
  95. cfg.should.have.property("projectPath")
  96. cfg.should.have.property("publicPath")
  97. cfg.config.should.have.property("basicAuth", "jabberwocky:skrillex")
  98. cfg.config.should.not.have.property("optionalThing")
  99. done()
  100. })
  101. })
  102. describe("prime(outputPath)", function(){
  103. before(function(done){
  104. fse.mkdirp(path.join(__dirname, "temp"), function(){
  105. fse.mkdirSync(path.join(__dirname, "temp", "myproj"))
  106. fse.mkdirSync(path.join(__dirname, "temp", "foo"))
  107. fse.writeFileSync(path.join(__dirname, "temp", "bar"), "hello bar")
  108. done()
  109. })
  110. })
  111. it("should only remove directories that do not begin with underscore", function(done){
  112. helpers.prime(path.join(__dirname, "temp"), { ignore: "myproj" }, function(error){
  113. fse.existsSync(path.join(__dirname, "temp", "myproj")).should.be.true
  114. fse.existsSync(path.join(__dirname, "temp", "foo")).should.be.false
  115. fse.existsSync(path.join(__dirname, "temp", "bar")).should.be.false
  116. done()
  117. })
  118. })
  119. after(function(done){
  120. fse.remove(path.join(__dirname, "temp"), done)
  121. })
  122. })
  123. })