forked from bellingard/sonar-scanner-npm
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e055920
commit d8fc335
Showing
9 changed files
with
886 additions
and
605 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* sonar-scanner-npm | ||
* Copyright (C) 2022-2024 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. | ||
*/ | ||
|
||
import fs from 'fs'; | ||
import { LogLevel, log } from './logging'; | ||
import { PlatformInfo, SupportedOS } from './types'; | ||
|
||
export function getArch(): NodeJS.Architecture { | ||
return process.arch; | ||
} | ||
|
||
function isLinux(): boolean { | ||
return process.platform.startsWith('linux'); | ||
} | ||
|
||
/** | ||
* @see https://github.com/microsoft/vscode/blob/64874113ad3c59e8d045f75dc2ef9d33d13f3a03/src/vs/platform/extensionManagement/common/extensionManagementUtil.ts#L171C1-L190C1 | ||
*/ | ||
|
||
function isAlpineLinux(): boolean { | ||
if (!isLinux()) { | ||
return false; | ||
} | ||
let content: string | undefined; | ||
try { | ||
const fileContent = fs.readFileSync('/etc/os-release'); | ||
content = fileContent.toString(); | ||
} catch (error) { | ||
try { | ||
const fileContent = fs.readFileSync('/usr/lib/os-release'); | ||
content = fileContent.toString(); | ||
} catch (error) { | ||
log(LogLevel.WARN, 'Failed to read /etc/os-release or /usr/lib/os-release'); | ||
} | ||
} | ||
return !!content && (content.match(/^ID=([^\u001b\r\n]*)/m) || [])[1] === 'alpine'; | ||
} | ||
|
||
function getSupportedOS(): SupportedOS { | ||
return isAlpineLinux() ? 'alpine' : process.platform; | ||
} | ||
|
||
export function getPlatformInfo(): PlatformInfo { | ||
return { | ||
os: getSupportedOS(), | ||
arch: getArch(), | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* sonar-scanner-npm | ||
* Copyright (C) 2022-2024 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. | ||
*/ | ||
|
||
import * as platform from '../../src/platform'; | ||
import * as logging from '../../src/logging'; | ||
import fs from 'fs'; | ||
import sinon from 'sinon'; | ||
|
||
describe('getPlatformInfo', () => { | ||
it('detect macos', () => { | ||
const platformStub = sinon.stub(process, 'platform').value('darwin'); | ||
const archStub = sinon.stub(process, 'arch').value('arm64'); | ||
|
||
expect(platform.getPlatformInfo()).toEqual({ | ||
os: 'darwin', | ||
arch: 'arm64', | ||
}); | ||
|
||
platformStub.restore(); | ||
archStub.restore(); | ||
}); | ||
|
||
it('detect windows', () => { | ||
const platformStub = sinon.stub(process, 'platform').value('win32'); | ||
const archStub = sinon.stub(process, 'arch').value('x64'); | ||
|
||
expect(platform.getPlatformInfo()).toEqual({ | ||
os: 'win32', | ||
arch: 'x64', | ||
}); | ||
|
||
platformStub.restore(); | ||
archStub.restore(); | ||
}); | ||
|
||
it('detect linux flavor', () => { | ||
const platformStub = sinon.stub(process, 'platform').value('openbsd'); | ||
const archStub = sinon.stub(process, 'arch').value('x64'); | ||
|
||
expect(platform.getPlatformInfo()).toEqual({ | ||
os: 'openbsd', | ||
arch: 'x64', | ||
}); | ||
|
||
platformStub.restore(); | ||
archStub.restore(); | ||
}); | ||
|
||
it('detect alpine', () => { | ||
const platformStub = sinon.stub(process, 'platform').value('linux'); | ||
const archStub = sinon.stub(process, 'arch').value('x64'); | ||
const fsReadStub = sinon.stub(fs, 'readFileSync'); | ||
fsReadStub.withArgs('/etc/os-release').returns('NAME="Alpine Linux"\nID=alpine'); | ||
|
||
expect(platform.getPlatformInfo()).toEqual({ | ||
os: 'alpine', | ||
arch: 'x64', | ||
}); | ||
|
||
platformStub.restore(); | ||
archStub.restore(); | ||
fsReadStub.restore(); | ||
}); | ||
|
||
it('detect alpine with fallback', () => { | ||
const platformStub = sinon.stub(process, 'platform').value('linux'); | ||
const archStub = sinon.stub(process, 'arch').value('x64'); | ||
const fsReadStub = sinon.stub(fs, 'readFileSync'); | ||
fsReadStub.withArgs('/usr/lib/os-release').returns('NAME="Alpine Linux"\nID=alpine'); | ||
|
||
expect(platform.getPlatformInfo()).toEqual({ | ||
os: 'alpine', | ||
arch: 'x64', | ||
}); | ||
|
||
platformStub.restore(); | ||
archStub.restore(); | ||
fsReadStub.restore(); | ||
}); | ||
|
||
it('failed to detect alpine', () => { | ||
const logSpy = sinon.spy(logging, 'log'); | ||
const platformStub = sinon.stub(process, 'platform').value('linux'); | ||
const archStub = sinon.stub(process, 'arch').value('x64'); | ||
|
||
expect(platform.getPlatformInfo()).toEqual({ | ||
os: 'linux', | ||
arch: 'x64', | ||
}); | ||
|
||
expect( | ||
logSpy.calledWith( | ||
logging.LogLevel.ERROR, | ||
'Failed to read /etc/os-release or /usr/lib/os-release', | ||
), | ||
).toBe(true); | ||
|
||
platformStub.restore(); | ||
archStub.restore(); | ||
logSpy.restore(); | ||
}); | ||
}); |