git-directory-helper.test.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. import * as core from '@actions/core'
  2. import * as fs from 'fs'
  3. import * as gitDirectoryHelper from '../lib/git-directory-helper'
  4. import * as io from '@actions/io'
  5. import * as path from 'path'
  6. import {IGitCommandManager} from '../lib/git-command-manager'
  7. const testWorkspace = path.join(__dirname, '_temp', 'git-directory-helper')
  8. let repositoryPath: string
  9. let httpsUrl: string
  10. let sshUrl: string
  11. let clean: boolean
  12. let git: IGitCommandManager
  13. describe('git-directory-helper tests', () => {
  14. beforeAll(async () => {
  15. // Clear test workspace
  16. await io.rmRF(testWorkspace)
  17. })
  18. beforeEach(() => {
  19. // Mock error/warning/info/debug
  20. jest.spyOn(core, 'error').mockImplementation(jest.fn())
  21. jest.spyOn(core, 'warning').mockImplementation(jest.fn())
  22. jest.spyOn(core, 'info').mockImplementation(jest.fn())
  23. jest.spyOn(core, 'debug').mockImplementation(jest.fn())
  24. })
  25. afterEach(() => {
  26. // Unregister mocks
  27. jest.restoreAllMocks()
  28. })
  29. const cleansWhenCleanTrue = 'cleans when clean true'
  30. it(cleansWhenCleanTrue, async () => {
  31. // Arrange
  32. await setup(cleansWhenCleanTrue)
  33. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  34. // Act
  35. await gitDirectoryHelper.prepareExistingDirectory(
  36. git,
  37. repositoryPath,
  38. httpsUrl,
  39. [httpsUrl, sshUrl],
  40. clean
  41. )
  42. // Assert
  43. const files = await fs.promises.readdir(repositoryPath)
  44. expect(files.sort()).toEqual(['.git', 'my-file'])
  45. expect(git.tryClean).toHaveBeenCalled()
  46. expect(git.tryReset).toHaveBeenCalled()
  47. expect(core.warning).not.toHaveBeenCalled()
  48. })
  49. const checkoutDetachWhenNotDetached = 'checkout detach when not detached'
  50. it(checkoutDetachWhenNotDetached, async () => {
  51. // Arrange
  52. await setup(checkoutDetachWhenNotDetached)
  53. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  54. // Act
  55. await gitDirectoryHelper.prepareExistingDirectory(
  56. git,
  57. repositoryPath,
  58. httpsUrl,
  59. [httpsUrl, sshUrl],
  60. clean
  61. )
  62. // Assert
  63. const files = await fs.promises.readdir(repositoryPath)
  64. expect(files.sort()).toEqual(['.git', 'my-file'])
  65. expect(git.checkoutDetach).toHaveBeenCalled()
  66. })
  67. const doesNotCheckoutDetachWhenNotAlreadyDetached =
  68. 'does not checkout detach when already detached'
  69. it(doesNotCheckoutDetachWhenNotAlreadyDetached, async () => {
  70. // Arrange
  71. await setup(doesNotCheckoutDetachWhenNotAlreadyDetached)
  72. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  73. const mockIsDetached = git.isDetached as jest.Mock<any, any>
  74. mockIsDetached.mockImplementation(async () => {
  75. return true
  76. })
  77. // Act
  78. await gitDirectoryHelper.prepareExistingDirectory(
  79. git,
  80. repositoryPath,
  81. httpsUrl,
  82. [httpsUrl, sshUrl],
  83. clean
  84. )
  85. // Assert
  86. const files = await fs.promises.readdir(repositoryPath)
  87. expect(files.sort()).toEqual(['.git', 'my-file'])
  88. expect(git.checkoutDetach).not.toHaveBeenCalled()
  89. })
  90. const doesNotCleanWhenCleanFalse = 'does not clean when clean false'
  91. it(doesNotCleanWhenCleanFalse, async () => {
  92. // Arrange
  93. await setup(doesNotCleanWhenCleanFalse)
  94. clean = false
  95. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  96. // Act
  97. await gitDirectoryHelper.prepareExistingDirectory(
  98. git,
  99. repositoryPath,
  100. httpsUrl,
  101. [httpsUrl, sshUrl],
  102. clean
  103. )
  104. // Assert
  105. const files = await fs.promises.readdir(repositoryPath)
  106. expect(files.sort()).toEqual(['.git', 'my-file'])
  107. expect(git.isDetached).toHaveBeenCalled()
  108. expect(git.branchList).toHaveBeenCalled()
  109. expect(core.warning).not.toHaveBeenCalled()
  110. expect(git.tryClean).not.toHaveBeenCalled()
  111. expect(git.tryReset).not.toHaveBeenCalled()
  112. })
  113. const removesContentsWhenCleanFails = 'removes contents when clean fails'
  114. it(removesContentsWhenCleanFails, async () => {
  115. // Arrange
  116. await setup(removesContentsWhenCleanFails)
  117. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  118. let mockTryClean = git.tryClean as jest.Mock<any, any>
  119. mockTryClean.mockImplementation(async () => {
  120. return false
  121. })
  122. // Act
  123. await gitDirectoryHelper.prepareExistingDirectory(
  124. git,
  125. repositoryPath,
  126. httpsUrl,
  127. [httpsUrl, sshUrl],
  128. clean
  129. )
  130. // Assert
  131. const files = await fs.promises.readdir(repositoryPath)
  132. expect(files).toHaveLength(0)
  133. expect(git.tryClean).toHaveBeenCalled()
  134. expect(core.warning).toHaveBeenCalled()
  135. expect(git.tryReset).not.toHaveBeenCalled()
  136. })
  137. const removesContentsWhenDifferentRepositoryUrl =
  138. 'removes contents when different repository url'
  139. it(removesContentsWhenDifferentRepositoryUrl, async () => {
  140. // Arrange
  141. await setup(removesContentsWhenDifferentRepositoryUrl)
  142. clean = false
  143. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  144. const differentRemoteUrl =
  145. 'https://github.com/my-different-org/my-different-repo'
  146. // Act
  147. await gitDirectoryHelper.prepareExistingDirectory(
  148. git,
  149. repositoryPath,
  150. differentRemoteUrl,
  151. [differentRemoteUrl],
  152. clean
  153. )
  154. // Assert
  155. const files = await fs.promises.readdir(repositoryPath)
  156. expect(files).toHaveLength(0)
  157. expect(core.warning).not.toHaveBeenCalled()
  158. expect(git.isDetached).not.toHaveBeenCalled()
  159. })
  160. const removesContentsWhenNoGitDirectory =
  161. 'removes contents when no git directory'
  162. it(removesContentsWhenNoGitDirectory, async () => {
  163. // Arrange
  164. await setup(removesContentsWhenNoGitDirectory)
  165. clean = false
  166. await io.rmRF(path.join(repositoryPath, '.git'))
  167. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  168. // Act
  169. await gitDirectoryHelper.prepareExistingDirectory(
  170. git,
  171. repositoryPath,
  172. httpsUrl,
  173. [httpsUrl, sshUrl],
  174. clean
  175. )
  176. // Assert
  177. const files = await fs.promises.readdir(repositoryPath)
  178. expect(files).toHaveLength(0)
  179. expect(core.warning).not.toHaveBeenCalled()
  180. expect(git.isDetached).not.toHaveBeenCalled()
  181. })
  182. const removesContentsWhenResetFails = 'removes contents when reset fails'
  183. it(removesContentsWhenResetFails, async () => {
  184. // Arrange
  185. await setup(removesContentsWhenResetFails)
  186. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  187. let mockTryReset = git.tryReset as jest.Mock<any, any>
  188. mockTryReset.mockImplementation(async () => {
  189. return false
  190. })
  191. // Act
  192. await gitDirectoryHelper.prepareExistingDirectory(
  193. git,
  194. repositoryPath,
  195. httpsUrl,
  196. [httpsUrl, sshUrl],
  197. clean
  198. )
  199. // Assert
  200. const files = await fs.promises.readdir(repositoryPath)
  201. expect(files).toHaveLength(0)
  202. expect(git.tryClean).toHaveBeenCalled()
  203. expect(git.tryReset).toHaveBeenCalled()
  204. expect(core.warning).toHaveBeenCalled()
  205. })
  206. const removesContentsWhenUndefinedGitCommandManager =
  207. 'removes contents when undefined git command manager'
  208. it(removesContentsWhenUndefinedGitCommandManager, async () => {
  209. // Arrange
  210. await setup(removesContentsWhenUndefinedGitCommandManager)
  211. clean = false
  212. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  213. // Act
  214. await gitDirectoryHelper.prepareExistingDirectory(
  215. undefined,
  216. repositoryPath,
  217. httpsUrl,
  218. [httpsUrl, sshUrl],
  219. clean
  220. )
  221. // Assert
  222. const files = await fs.promises.readdir(repositoryPath)
  223. expect(files).toHaveLength(0)
  224. expect(core.warning).not.toHaveBeenCalled()
  225. })
  226. const removesLocalBranches = 'removes local branches'
  227. it(removesLocalBranches, async () => {
  228. // Arrange
  229. await setup(removesLocalBranches)
  230. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  231. const mockBranchList = git.branchList as jest.Mock<any, any>
  232. mockBranchList.mockImplementation(async (remote: boolean) => {
  233. return remote ? [] : ['local-branch-1', 'local-branch-2']
  234. })
  235. // Act
  236. await gitDirectoryHelper.prepareExistingDirectory(
  237. git,
  238. repositoryPath,
  239. httpsUrl,
  240. [httpsUrl, sshUrl],
  241. clean
  242. )
  243. // Assert
  244. const files = await fs.promises.readdir(repositoryPath)
  245. expect(files.sort()).toEqual(['.git', 'my-file'])
  246. expect(git.branchDelete).toHaveBeenCalledWith(false, 'local-branch-1')
  247. expect(git.branchDelete).toHaveBeenCalledWith(false, 'local-branch-2')
  248. })
  249. const removesLockFiles = 'removes lock files'
  250. it(removesLockFiles, async () => {
  251. // Arrange
  252. await setup(removesLockFiles)
  253. clean = false
  254. await fs.promises.writeFile(
  255. path.join(repositoryPath, '.git', 'index.lock'),
  256. ''
  257. )
  258. await fs.promises.writeFile(
  259. path.join(repositoryPath, '.git', 'shallow.lock'),
  260. ''
  261. )
  262. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  263. // Act
  264. await gitDirectoryHelper.prepareExistingDirectory(
  265. git,
  266. repositoryPath,
  267. httpsUrl,
  268. [httpsUrl, sshUrl],
  269. clean
  270. )
  271. // Assert
  272. let files = await fs.promises.readdir(path.join(repositoryPath, '.git'))
  273. expect(files).toHaveLength(0)
  274. files = await fs.promises.readdir(repositoryPath)
  275. expect(files.sort()).toEqual(['.git', 'my-file'])
  276. expect(git.isDetached).toHaveBeenCalled()
  277. expect(git.branchList).toHaveBeenCalled()
  278. expect(core.warning).not.toHaveBeenCalled()
  279. expect(git.tryClean).not.toHaveBeenCalled()
  280. expect(git.tryReset).not.toHaveBeenCalled()
  281. })
  282. const removesRemoteBranches = 'removes local branches'
  283. it(removesRemoteBranches, async () => {
  284. // Arrange
  285. await setup(removesRemoteBranches)
  286. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  287. const mockBranchList = git.branchList as jest.Mock<any, any>
  288. mockBranchList.mockImplementation(async (remote: boolean) => {
  289. return remote ? ['remote-branch-1', 'remote-branch-2'] : []
  290. })
  291. // Act
  292. await gitDirectoryHelper.prepareExistingDirectory(
  293. git,
  294. repositoryPath,
  295. httpsUrl,
  296. [httpsUrl, sshUrl],
  297. clean
  298. )
  299. // Assert
  300. const files = await fs.promises.readdir(repositoryPath)
  301. expect(files.sort()).toEqual(['.git', 'my-file'])
  302. expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-1')
  303. expect(git.branchDelete).toHaveBeenCalledWith(true, 'remote-branch-2')
  304. })
  305. const updatesRemoteUrl = 'updates remote URL'
  306. it(updatesRemoteUrl, async () => {
  307. // Arrange
  308. await setup(updatesRemoteUrl)
  309. await fs.promises.writeFile(path.join(repositoryPath, 'my-file'), '')
  310. // Act
  311. await gitDirectoryHelper.prepareExistingDirectory(
  312. git,
  313. repositoryPath,
  314. sshUrl,
  315. [sshUrl, httpsUrl],
  316. clean
  317. )
  318. // Assert
  319. const files = await fs.promises.readdir(repositoryPath)
  320. expect(files.sort()).toEqual(['.git', 'my-file'])
  321. expect(git.isDetached).toHaveBeenCalled()
  322. expect(git.branchList).toHaveBeenCalled()
  323. expect(core.warning).not.toHaveBeenCalled()
  324. expect(git.setRemoteUrl).toHaveBeenCalledWith(sshUrl)
  325. })
  326. })
  327. async function setup(testName: string): Promise<void> {
  328. testName = testName.replace(/[^a-zA-Z0-9_]+/g, '-')
  329. // Repository directory
  330. repositoryPath = path.join(testWorkspace, testName)
  331. await fs.promises.mkdir(path.join(repositoryPath, '.git'), {recursive: true})
  332. // Remote URLs
  333. httpsUrl = 'https://github.com/my-org/my-repo'
  334. sshUrl = 'git@github.com:my-org/my-repo'
  335. // Clean
  336. clean = true
  337. // Git command manager
  338. git = {
  339. branchDelete: jest.fn(),
  340. branchExists: jest.fn(),
  341. branchList: jest.fn(async () => {
  342. return []
  343. }),
  344. checkout: jest.fn(),
  345. checkoutDetach: jest.fn(),
  346. config: jest.fn(),
  347. configExists: jest.fn(),
  348. fetch: jest.fn(),
  349. getWorkingDirectory: jest.fn(() => repositoryPath),
  350. init: jest.fn(),
  351. isDetached: jest.fn(),
  352. lfsFetch: jest.fn(),
  353. lfsInstall: jest.fn(),
  354. log1: jest.fn(),
  355. remoteAdd: jest.fn(),
  356. removeEnvironmentVariable: jest.fn(),
  357. setEnvironmentVariable: jest.fn(),
  358. setRemoteUrl: jest.fn(),
  359. submoduleForeach: jest.fn(),
  360. submoduleSync: jest.fn(),
  361. submoduleUpdate: jest.fn(),
  362. tagExists: jest.fn(),
  363. tryClean: jest.fn(async () => {
  364. return true
  365. }),
  366. tryConfigUnset: jest.fn(),
  367. tryDisableAutomaticGarbageCollection: jest.fn(),
  368. tryGetRemoteUrl: jest.fn(async () => {
  369. // Sanity check - this function shouldn't be called when the .git directory doesn't exist
  370. await fs.promises.stat(path.join(repositoryPath, '.git'))
  371. return httpsUrl
  372. }),
  373. tryReset: jest.fn(async () => {
  374. return true
  375. })
  376. }
  377. }