summaryrefslogtreecommitdiff
path: root/test/test_temporary_file_management.vader
blob: ae2bf251aa31fdafce3056a0afe16b20e5b6c2fa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Before:
  let g:ale_run_synchronously = 1

  let g:command = 'echo test'
  let g:filename = ''
  let g:directory = ''
  let g:preserved_directory = ''

  function! TestCommandCallback(buffer) abort
    " We are registering a temporary file, so we should delete it.
    let g:filename = tempname()
    call writefile(['foo'], g:filename)
    call ale#engine#ManageFile(a:buffer, g:filename)

    " We are registering this directory appropriately, so we should delete
    " the whole thing.
    let g:directory = tempname()
    call mkdir(g:directory)
    call writefile(['foo'], g:directory . '/bar')
    call ale#engine#ManageDirectory(a:buffer, g:directory)

    " We are registering this directory as temporary file, so we
    " shouldn't delete it.
    let g:preserved_directory = tempname()
    call mkdir(g:preserved_directory)
    call writefile(['foo'], g:preserved_directory . '/bar')
    call ale#engine#ManageFile(a:buffer, g:preserved_directory)

    return g:command
  endfunction

  function! TestCallback(buffer, output) abort
    return []
  endfunction

  call ale#linter#Define('foobar', {
  \ 'name': 'testlinter',
  \ 'executable': has('win32') ? 'cmd' : 'echo',
  \ 'callback': 'TestCallback',
  \ 'command_callback': 'TestCommandCallback',
  \})

After:
  if !empty(g:preserved_directory)
    call delete(g:preserved_directory, 'rf')
  endif

  unlet! g:ale_run_synchronously
  unlet! g:command
  unlet! g:filename
  unlet! g:directory
  unlet! g:preserved_directory
  delfunction TestCommandCallback
  delfunction TestCallback
  call ale#linter#Reset()

Given foobar (Some imaginary filetype):
  foo
  bar
  baz

Execute(ALE should delete managed files/directories appropriately after linting):
  AssertEqual 'foobar', &filetype

  call ale#Lint()
  call ale#engine#WaitForJobs(2000)

  Assert !filereadable(g:filename), 'The temporary file was not deleted'
  Assert !isdirectory(g:directory), 'The temporary directory was not deleted'
  Assert isdirectory(g:preserved_directory), 'The temporary directory was not kept'

Execute(ALE should delete managed files even if no command is run):
  AssertEqual 'foobar', &filetype

  let g:command = ''

  call ale#Lint()
  call ale#engine#WaitForJobs(2000)

  Assert !filereadable(g:filename), 'The temporary file was not deleted'
  Assert !isdirectory(g:directory), 'The temporary directory was not deleted'
  Assert isdirectory(g:preserved_directory), 'The temporary directory was not kept'

Execute(ALE should delete managed files when the buffer is removed):
  call ale#engine#InitBufferInfo(bufnr('%'))
  call TestCommandCallback(bufnr('%'))
  call ale#engine#Cleanup(bufnr('%'))

  Assert !filereadable(g:filename), 'The temporary file was not deleted'
  Assert !isdirectory(g:directory), 'The temporary directory was not deleted'
  Assert isdirectory(g:preserved_directory), 'The tempoary directory was not kept'

Execute(ALE should create and delete directories for ale#engine#CreateDirectory()):
  call ale#engine#InitBufferInfo(bufnr('%'))

  let b:dir = ale#engine#CreateDirectory(bufnr('%'))
  let b:dir2 = ale#engine#CreateDirectory(bufnr('%'))

  Assert isdirectory(b:dir), 'The directory was not created'

  " We should get the correct file permissions.
  " We want to ensure that the directory is not readable by 'other'
  if has('unix')
    AssertEqual 'rwxr-x---', getfperm(b:dir)
  endif

  " The two directories shouldn't be the same.
  AssertNotEqual b:dir2, b:dir

  call ale#engine#Cleanup(bufnr('%'))

  Assert !isdirectory(b:dir), 'The directory was not deleted'
  Assert !isdirectory(b:dir2), 'The second directory was not deleted'