buildDir.test.ts 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import * as fs from 'fs'
  2. import * as os from 'os'
  3. import * as path from 'path'
  4. import { afterAll, describe, expect, it } from 'vitest'
  5. import { isOwnDirectory, lweBuildDir } from './buildDir'
  6. describe('lweBuildDir', () => {
  7. it('is in the cache folder of the user, never in /tmp', () => {
  8. expect(lweBuildDir({}, '/home/u')).toBe('/home/u/.cache/we-manager/lwe-build')
  9. expect(lweBuildDir({ XDG_CACHE_HOME: '/data/cache' }, '/home/u')).toBe('/data/cache/we-manager/lwe-build')
  10. })
  11. it('ignores a cache variable that is not an absolute path', () => {
  12. expect(lweBuildDir({ XDG_CACHE_HOME: 'relative/cache' }, '/home/u')).toBe('/home/u/.cache/we-manager/lwe-build')
  13. expect(lweBuildDir({ XDG_CACHE_HOME: '' }, '/home/u')).toBe('/home/u/.cache/we-manager/lwe-build')
  14. })
  15. })
  16. describe('isOwnDirectory', () => {
  17. const tmp = fs.mkdtempSync(path.join(os.tmpdir(), 'wem-builddir-'))
  18. afterAll(() => fs.rmSync(tmp, { recursive: true, force: true }))
  19. it('accepts a directory of the user', () => {
  20. const dir = path.join(tmp, 'mine')
  21. fs.mkdirSync(dir)
  22. expect(isOwnDirectory(dir)).toBe(true)
  23. })
  24. it('refuses one that belongs to somebody else', () => {
  25. const uid = process.getuid!()
  26. expect(isOwnDirectory(tmp, uid + 1)).toBe(false)
  27. expect(isOwnDirectory('/', uid === 0 ? 1 : uid)).toBe(false)
  28. })
  29. it('refuses a link, a file and what is not there', () => {
  30. const target = path.join(tmp, 'target')
  31. fs.mkdirSync(target)
  32. fs.symlinkSync(target, path.join(tmp, 'link'))
  33. fs.writeFileSync(path.join(tmp, 'file'), '')
  34. expect(isOwnDirectory(path.join(tmp, 'link'))).toBe(false)
  35. expect(isOwnDirectory(path.join(tmp, 'file'))).toBe(false)
  36. expect(isOwnDirectory(path.join(tmp, 'nothing'))).toBe(false)
  37. })
  38. })