aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-scanner-engine/src/test/java/org/sonar/scanner/scan/WorkDirectoriesInitializerTest.java
blob: 542a50c4e9c57238293868db706e5b0caac93257 (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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
 * SonarQube
 * Copyright (C) 2009-2025 SonarSource SA
 * mailto:info AT sonarsource DOT com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package org.sonar.scanner.scan;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.sonar.api.batch.fs.internal.DefaultInputModule;
import org.sonar.api.batch.fs.internal.DefaultInputProject;
import org.sonar.scanner.fs.InputModuleHierarchy;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class WorkDirectoriesInitializerTest {
  private final WorkDirectoriesInitializer initializer = new WorkDirectoriesInitializer();
  private final InputModuleHierarchy hierarchy = mock(InputModuleHierarchy.class);
  private final DefaultInputProject project = mock(DefaultInputProject.class);
  private final DefaultInputModule root = mock(DefaultInputModule.class);

  @Rule
  public TemporaryFolder temp = new TemporaryFolder();

  private File rootWorkDir;
  private File lock;

  @Before
  public void setUp() throws IOException {
    rootWorkDir = temp.newFolder();
    when(hierarchy.root()).thenReturn(root);
    createFilesToClean(rootWorkDir);
    when(root.getWorkDir()).thenReturn(rootWorkDir.toPath());
    when(project.getWorkDir()).thenReturn(rootWorkDir.toPath());
  }

  private void createFilesToClean(File dir) throws IOException {
    new File(dir, "foo.txt").createNewFile();
    File newFolder = new File(dir, "foo");
    newFolder.mkdir();
    File fileInFolder = new File(newFolder, "test");
    fileInFolder.createNewFile();

    lock = new File(dir, DirectoryLock.LOCK_FILE_NAME);
    lock.createNewFile();
    assertThat(dir.list()).hasSizeGreaterThan(1);
  }

  @Test
  public void execute_doesnt_fail_if_nothing_to_clean() {
    temp.delete();
    initializer.execute(hierarchy);
  }

  @Test
  public void execute_should_clean_root() {
    initializer.execute(project);

    assertThat(rootWorkDir).exists();
    assertThat(lock).exists();
    assertThat(rootWorkDir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME);
  }

  @Test
  public void execute_on_hierarchy_should_clean_submodules() throws IOException {
    DefaultInputModule moduleA = mock(DefaultInputModule.class);
    DefaultInputModule moduleB = mock(DefaultInputModule.class);

    when(hierarchy.children(root)).thenReturn(Arrays.asList(moduleA));
    when(hierarchy.children(moduleA)).thenReturn(Arrays.asList(moduleB));

    File moduleAWorkdir = new File(rootWorkDir, "moduleA");
    File moduleBWorkdir = new File(moduleAWorkdir, "moduleB");

    when(moduleA.getWorkDir()).thenReturn(moduleAWorkdir.toPath());
    when(moduleB.getWorkDir()).thenReturn(moduleBWorkdir.toPath());

    moduleAWorkdir.mkdir();
    moduleBWorkdir.mkdir();

    new File(moduleAWorkdir, "fooA.txt").createNewFile();
    new File(moduleBWorkdir, "fooB.txt").createNewFile();

    initializer.execute(project);
    initializer.execute(hierarchy);

    assertThat(rootWorkDir).exists();
    assertThat(lock).exists();
    assertThat(rootWorkDir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME, "moduleA");
    assertThat(moduleAWorkdir).exists();
    assertThat(moduleBWorkdir).exists();
    assertThat(moduleAWorkdir.list()).containsOnly("moduleB");
    assertThat(moduleBWorkdir).isEmptyDirectory();
  }

  @Test
  public void execute_on_hierarchy_should_clean_submodules_expect_submodule_with_same_work_directory_as_root() throws IOException {
    DefaultInputModule moduleA = mock(DefaultInputModule.class);
    DefaultInputModule moduleB = mock(DefaultInputModule.class);

    when(hierarchy.children(root)).thenReturn(List.of(moduleA, moduleB));

    File rootAndModuleAWorkdir = new File(rootWorkDir, "moduleA");
    File moduleBWorkdir = new File(rootAndModuleAWorkdir, "../moduleB");

    when(root.getWorkDir()).thenReturn(rootAndModuleAWorkdir.toPath());
    when(moduleA.getWorkDir()).thenReturn(rootAndModuleAWorkdir.toPath());
    when(moduleB.getWorkDir()).thenReturn(moduleBWorkdir.toPath());

    rootAndModuleAWorkdir.mkdir();
    createFilesToClean(rootAndModuleAWorkdir);
    moduleBWorkdir.mkdir();

    new File(rootAndModuleAWorkdir, "fooA.txt").createNewFile();
    new File(moduleBWorkdir, "fooB.txt").createNewFile();

    initializer.execute(hierarchy);

    assertThat(rootWorkDir).exists();
    assertThat(lock).exists();
    assertThat(rootAndModuleAWorkdir).exists();
    assertThat(rootAndModuleAWorkdir.list()).containsOnly(DirectoryLock.LOCK_FILE_NAME, "fooA.txt", "foo", "foo.txt");
    assertThat(moduleBWorkdir)
      .exists()
      .isEmptyDirectory();
  }

}