-
Notifications
You must be signed in to change notification settings - Fork 14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SCANNPM-2 Detect Platform #110
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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(), | ||
}; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,18 @@ | |
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
|
||
export type ScanOptions = { | ||
serverUrl: string; | ||
token: string; | ||
jvmOptions: string[]; | ||
options?: { [key: string]: string }; | ||
caPath: string; | ||
logLevel?: string; | ||
verbose?: boolean; | ||
}; | ||
import { log, LogLevel } from './logging'; | ||
import { getPlatformInfo } from './platform'; | ||
import { ScanOptions } from './types'; | ||
|
||
export async function scan(scanOptions: ScanOptions, cliArgs?: string[]) { | ||
// TODO: NPMSCAN-2 new bootstrapper sequence | ||
log(LogLevel.DEBUG, 'Finding platform info'); | ||
const platformInfo = getPlatformInfo(); | ||
log(LogLevel.INFO, 'Platform: ', platformInfo); | ||
Comment on lines
+26
to
+28
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to conditionally skip this with the presence of @7PH let me know if you have any process or suggestion on how we want to do this 🙇🏻 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. After this PR you'll get the |
||
|
||
//TODO: verifyJRE based on platform | ||
//TODO: fetchJRE | ||
//TODO: verifyScannerEngine | ||
//TODO: fetchScannerEngine | ||
//TODO: | ||
} |
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(); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added support for ts, but I think this won't work with the js tests that exist. so need to cleanup and make sure we are left with no js tests 😅