index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. 'use strict'
  2. const fs = require('fs')
  3. const path = require('path')
  4. const mkdirp = require('mkdirp')
  5. const rimraf = require('rimraf')
  6. const tempy = require('tempy')
  7. const installFrom = require('../src/install')
  8. const uninstallFrom = require('../src/uninstall')
  9. function install(rootDir, dir) {
  10. installFrom(path.join(rootDir, dir))
  11. }
  12. function uninstall(rootDir, dir) {
  13. uninstallFrom(path.join(rootDir, dir))
  14. }
  15. function mkdir(rootDir, dir) {
  16. mkdirp.sync(path.join(rootDir, dir))
  17. }
  18. function writeFile(dir, filePath, data) {
  19. fs.writeFileSync(path.join(dir, filePath), data)
  20. }
  21. function readFile(dir, filePath) {
  22. return fs.readFileSync(path.join(dir, filePath), 'utf-8')
  23. }
  24. function exists(dir, filePath) {
  25. return fs.existsSync(path.join(dir, filePath))
  26. }
  27. describe('yorkie', () => {
  28. let dir
  29. beforeEach(() => (dir = tempy.directory()))
  30. afterEach(() => rimraf.sync(dir))
  31. it('should support basic layout', () => {
  32. mkdir(dir, '.git/hooks')
  33. mkdir(dir, 'node_modules/yorkie')
  34. writeFile(dir, 'package.json', '{}')
  35. install(dir, '/node_modules/yorkie')
  36. const hook = readFile(dir, '.git/hooks/pre-commit')
  37. expect(hook).toMatch('#yorkie')
  38. expect(hook).toMatch('cd "."')
  39. expect(hook).toMatch(`node "./node_modules/yorkie/src/runner.js" pre-commit`)
  40. expect(hook).toMatch('--no-verify')
  41. const prepareCommitMsg = readFile(dir, '.git/hooks/prepare-commit-msg')
  42. expect(prepareCommitMsg).toMatch('cannot be bypassed')
  43. uninstall(dir, 'node_modules/yorkie')
  44. expect(exists(dir, '.git/hooks/pre-push')).toBeFalsy()
  45. })
  46. it('should not install git hooks when installed in sub directory', () => {
  47. mkdir(dir, '.git/hooks')
  48. mkdir(dir, 'A/B/node_modules/yorkie')
  49. writeFile(dir, 'A/B/package.json', '{}')
  50. install(dir, 'A/B/node_modules/yorkie')
  51. expect(exists(dir, '.git/hooks/pre-commit')).toBeFalsy()
  52. })
  53. it('should support git submodule', () => {
  54. mkdir(dir, '.git/modules/A/B')
  55. mkdir(dir, 'A/B/node_modules/yorkie')
  56. writeFile(dir, 'package.json', '{}')
  57. writeFile(dir, 'A/B/package.json', '{}')
  58. writeFile(dir, 'A/B/.git', 'git: ../../.git/modules/A/B')
  59. install(dir, 'A/B/node_modules/yorkie')
  60. const hook = readFile(dir, '.git/modules/A/B/hooks/pre-commit')
  61. expect(hook).toMatch('cd "."')
  62. uninstall(dir, 'A/B/node_modules/yorkie')
  63. expect(exists(dir, '.git/hooks/pre-push')).toBeFalsy()
  64. })
  65. it('should not install git hooks in submodule sub directory', () => {
  66. mkdir(dir, '.git/modules/A/B')
  67. mkdir(dir, 'A/B/C/node_modules/yorkie')
  68. writeFile(dir, 'package.json', '{}')
  69. writeFile(dir, 'A/B/C/package.json', '{}')
  70. writeFile(dir, 'A/B/.git', 'git: ../../.git/modules/A/B')
  71. install(dir, 'A/B/C/node_modules/yorkie')
  72. expect(exists(dir, '.git/modules/A/B/hooks/pre-commit')).toBeFalsy()
  73. })
  74. it('should support git worktrees', () => {
  75. mkdir(dir, '.git/worktrees/B')
  76. mkdir(dir, 'A/B/node_modules/yorkie')
  77. writeFile(dir, 'package.json', '{}')
  78. writeFile(dir, 'A/B/package.json', '{}')
  79. // Git path for worktrees is absolute
  80. const absolutePath = path.join(dir, '.git/worktrees/B')
  81. writeFile(dir, 'A/B/.git', `git: ${absolutePath}`)
  82. install(dir, 'A/B/node_modules/yorkie')
  83. const hook = readFile(dir, '.git/worktrees/B/hooks/pre-commit')
  84. expect(hook).toMatch('cd "."')
  85. uninstall(dir, 'A/B/node_modules/yorkie')
  86. expect(exists(dir, '.git/hooks/pre-commit')).toBeFalsy()
  87. })
  88. it('should not modify user hooks', () => {
  89. mkdir(dir, '.git/hooks')
  90. mkdir(dir, 'node_modules/yorkie')
  91. writeFile(dir, '.git/hooks/pre-push', 'foo')
  92. // Verify that it's not overwritten
  93. install(dir, 'node_modules/yorkie')
  94. const hook = readFile(dir, '.git/hooks/pre-push')
  95. expect(hook).toBe('foo')
  96. uninstall(dir, 'node_modules/yorkie')
  97. expect(exists(dir, '.git/hooks/pre-push')).toBeTruthy()
  98. })
  99. it('should not install from /node_modules/A/node_modules', () => {
  100. mkdir(dir, '.git/hooks')
  101. mkdir(dir, 'node_modules/A/node_modules/yorkie')
  102. install(dir, 'node_modules/A/node_modules/yorkie')
  103. expect(exists(dir, '.git/hooks/pre-push')).toBeFalsy()
  104. })
  105. it("should not crash if there's no .git directory", () => {
  106. mkdir(dir, 'node_modules/yorkie')
  107. expect(() => install(dir, 'node_modules/yorkie')).not.toThrow()
  108. expect(() => uninstall(dir, 'node_modules/yorkie')).not.toThrow()
  109. })
  110. it('should migrate existing scripts (ghooks)', () => {
  111. mkdir(dir, '.git/hooks')
  112. writeFile(dir, 'package.json', '{}')
  113. mkdir(dir, '/node_modules/yorkie')
  114. writeFile(
  115. dir,
  116. '.git/hooks/pre-commit',
  117. '// Generated by ghooks. Do not edit this file.'
  118. )
  119. install(dir, 'node_modules/yorkie')
  120. const hook = readFile(dir, '.git/hooks/pre-commit')
  121. expect(hook).toMatch('yorkie')
  122. expect(hook).not.toMatch('ghooks')
  123. })
  124. it('should migrate existing scripts (pre-commit)', () => {
  125. mkdir(dir, '.git/hooks')
  126. writeFile(dir, 'package.json', '{}')
  127. mkdir(dir, '/node_modules/yorkie')
  128. writeFile(dir, '.git/hooks/pre-commit', './node_modules/pre-commit/hook')
  129. install(dir, 'node_modules/yorkie')
  130. const hook = readFile(dir, '.git/hooks/pre-commit')
  131. expect(hook).toMatch('yorkie')
  132. expect(hook).not.toMatch('./node_modules/pre-commit/hook')
  133. })
  134. })