1+ /*
2+ * SonarScanner Java Library
3+ * Copyright (C) 2011-2024 SonarSource SA
4+ * mailto:info AT sonarsource DOT com
5+ *
6+ * This program is free software; you can redistribute it and/or
7+ * modify it under the terms of the GNU Lesser General Public
8+ * License as published by the Free Software Foundation; either
9+ * version 3 of the License, or (at your option) any later version.
10+ *
11+ * This program is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+ * Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public License
17+ * along with this program; if not, write to the Free Software Foundation,
18+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+ */
20+ package org .sonarsource .scanner .lib .internal ;
21+
22+ import java .io .ByteArrayInputStream ;
23+ import java .io .IOException ;
24+ import org .junit .jupiter .api .Test ;
25+ import org .junit .jupiter .api .extension .RegisterExtension ;
26+ import org .slf4j .event .Level ;
27+ import org .sonarsource .scanner .lib .System2 ;
28+ import testutils .LogTester ;
29+
30+ import static org .assertj .core .api .Assertions .assertThat ;
31+ import static org .mockito .Mockito .mock ;
32+ import static org .mockito .Mockito .when ;
33+
34+ class ArchResolverTest {
35+
36+ @ RegisterExtension
37+ private final LogTester logTester = new LogTester ().setLevel (Level .DEBUG );
38+
39+ @ Test
40+ void shouldUseUnameToDetectArch () throws IOException {
41+ var processWrapperFactory = mock (ProcessWrapperFactory .class );
42+ var process = mock (ProcessWrapperFactory .ProcessWrapper .class );
43+ when (process .getInputStream ()).thenReturn (new ByteArrayInputStream ("arm64" .getBytes ()));
44+ when (processWrapperFactory .create ("uname" , "-m" )).thenReturn (process );
45+ ArchResolver archResolver = new ArchResolver (new System2 (), processWrapperFactory , true );
46+
47+ String arch = archResolver .getCpuArch ();
48+
49+ assertThat (arch ).isEqualTo ("arm64" );
50+ assertThat (logTester .logs (Level .DEBUG )).contains ("uname -m returned 'arm64'" );
51+ }
52+
53+ @ Test
54+ void shouldNotUseUnameOnWindows () {
55+ ArchResolver archResolver = new ArchResolver (new System2 (), mock (ProcessWrapperFactory .class ), false );
56+
57+ String arch = archResolver .getCpuArch ();
58+
59+ assertThat (arch ).isEqualTo (System .getProperty ("os.arch" ));
60+ }
61+
62+ @ Test
63+ void shouldFallbackToSystemPropertiesIfExitIsNotZero () throws IOException , InterruptedException {
64+ var processWrapperFactory = mock (ProcessWrapperFactory .class );
65+ var process = mock (ProcessWrapperFactory .ProcessWrapper .class );
66+ when (process .getInputStream ()).thenReturn (new ByteArrayInputStream ("Error" .getBytes ()));
67+ when (process .waitFor ()).thenReturn (-1 );
68+ when (processWrapperFactory .create ("uname" , "-m" )).thenReturn (process );
69+ ArchResolver archResolver = new ArchResolver (new System2 (), processWrapperFactory , true );
70+
71+ String arch = archResolver .getCpuArch ();
72+
73+ assertThat (arch ).isEqualTo (System .getProperty ("os.arch" ));
74+ assertThat (logTester .logs (Level .DEBUG )).contains ("uname -m returned 'Error'" , "Command exited with code: -1" );
75+ }
76+
77+ @ Test
78+ void shouldFallbackToSystemPropertiesIfExceptionOnWaitFor () throws IOException , InterruptedException {
79+ var processWrapperFactory = mock (ProcessWrapperFactory .class );
80+ var process = mock (ProcessWrapperFactory .ProcessWrapper .class );
81+ when (process .getInputStream ()).thenReturn (new ByteArrayInputStream ("Error" .getBytes ()));
82+ when (process .waitFor ()).thenThrow (new InterruptedException ());
83+ when (processWrapperFactory .create ("uname" , "-m" )).thenReturn (process );
84+ ArchResolver archResolver = new ArchResolver (new System2 (), processWrapperFactory , true );
85+
86+ String arch = archResolver .getCpuArch ();
87+
88+ assertThat (arch ).isEqualTo (System .getProperty ("os.arch" ));
89+ assertThat (logTester .logs (Level .DEBUG )).contains ("Failed to get the architecture using 'uname -m'" );
90+ }
91+
92+ @ Test
93+ void shouldFallbackToSystemPropertiesIfNoOutput () throws IOException {
94+ var processWrapperFactory = mock (ProcessWrapperFactory .class );
95+ var process = mock (ProcessWrapperFactory .ProcessWrapper .class );
96+ when (process .getInputStream ()).thenReturn (new ByteArrayInputStream (new byte [0 ]));
97+ when (processWrapperFactory .create ("uname" , "-m" )).thenReturn (process );
98+ ArchResolver archResolver = new ArchResolver (new System2 (), processWrapperFactory , true );
99+
100+ String arch = archResolver .getCpuArch ();
101+
102+ assertThat (arch ).isEqualTo (System .getProperty ("os.arch" ));
103+ assertThat (logTester .logs (Level .DEBUG )).contains ("Failed to get the architecture using 'uname -m'" );
104+ }
105+ }
0 commit comments