test-find-visualstudio.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. 'use strict'
  2. const test = require('tap').test
  3. const fs = require('fs')
  4. const path = require('path')
  5. const findVisualStudio = require('../lib/find-visualstudio')
  6. const VisualStudioFinder = findVisualStudio.test.VisualStudioFinder
  7. const semverV1 = { major: 1, minor: 0, patch: 0 }
  8. delete process.env.VCINSTALLDIR
  9. function poison (object, property) {
  10. function fail () {
  11. console.error(Error(`Property ${property} should not have been accessed.`))
  12. process.abort()
  13. }
  14. var descriptor = {
  15. configurable: false,
  16. enumerable: false,
  17. get: fail,
  18. set: fail
  19. }
  20. Object.defineProperty(object, property, descriptor)
  21. }
  22. function TestVisualStudioFinder () { VisualStudioFinder.apply(this, arguments) }
  23. TestVisualStudioFinder.prototype = Object.create(VisualStudioFinder.prototype)
  24. // Silence npmlog - remove for debugging
  25. TestVisualStudioFinder.prototype.log = {
  26. silly: () => {},
  27. verbose: () => {},
  28. info: () => {},
  29. warn: () => {},
  30. error: () => {}
  31. }
  32. test('VS2013', function (t) {
  33. t.plan(4)
  34. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  35. t.strictEqual(err, null)
  36. t.deepEqual(info, {
  37. msBuild: 'C:\\MSBuild12\\MSBuild.exe',
  38. path: 'C:\\VS2013',
  39. sdk: null,
  40. toolset: 'v120',
  41. version: '12.0',
  42. versionMajor: 12,
  43. versionMinor: 0,
  44. versionYear: 2013
  45. })
  46. })
  47. finder.findVisualStudio2017OrNewer = (cb) => {
  48. finder.parseData(new Error(), '', '', cb)
  49. }
  50. finder.regSearchKeys = (keys, value, addOpts, cb) => {
  51. for (var i = 0; i < keys.length; ++i) {
  52. const fullName = `${keys[i]}\\${value}`
  53. switch (fullName) {
  54. case 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  55. case 'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  56. continue
  57. case 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7\\12.0':
  58. t.pass(`expected search for registry value ${fullName}`)
  59. return cb(null, 'C:\\VS2013\\VC\\')
  60. case 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions\\12.0\\MSBuildToolsPath':
  61. t.pass(`expected search for registry value ${fullName}`)
  62. return cb(null, 'C:\\MSBuild12\\')
  63. default:
  64. t.fail(`unexpected search for registry value ${fullName}`)
  65. }
  66. }
  67. return cb(new Error())
  68. }
  69. finder.findVisualStudio()
  70. })
  71. test('VS2013 should not be found on new node versions', function (t) {
  72. t.plan(2)
  73. const finder = new TestVisualStudioFinder({
  74. major: 10,
  75. minor: 0,
  76. patch: 0
  77. }, null, (err, info) => {
  78. t.ok(/find .* Visual Studio/i.test(err), 'expect error')
  79. t.false(info, 'no data')
  80. })
  81. finder.findVisualStudio2017OrNewer = (cb) => {
  82. const file = path.join(__dirname, 'fixtures', 'VS_2017_Unusable.txt')
  83. const data = fs.readFileSync(file)
  84. finder.parseData(null, data, '', cb)
  85. }
  86. finder.regSearchKeys = (keys, value, addOpts, cb) => {
  87. for (var i = 0; i < keys.length; ++i) {
  88. const fullName = `${keys[i]}\\${value}`
  89. switch (fullName) {
  90. case 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  91. case 'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  92. continue
  93. default:
  94. t.fail(`unexpected search for registry value ${fullName}`)
  95. }
  96. }
  97. return cb(new Error())
  98. }
  99. finder.findVisualStudio()
  100. })
  101. test('VS2015', function (t) {
  102. t.plan(4)
  103. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  104. t.strictEqual(err, null)
  105. t.deepEqual(info, {
  106. msBuild: 'C:\\MSBuild14\\MSBuild.exe',
  107. path: 'C:\\VS2015',
  108. sdk: null,
  109. toolset: 'v140',
  110. version: '14.0',
  111. versionMajor: 14,
  112. versionMinor: 0,
  113. versionYear: 2015
  114. })
  115. })
  116. finder.findVisualStudio2017OrNewer = (cb) => {
  117. finder.parseData(new Error(), '', '', cb)
  118. }
  119. finder.regSearchKeys = (keys, value, addOpts, cb) => {
  120. for (var i = 0; i < keys.length; ++i) {
  121. const fullName = `${keys[i]}\\${value}`
  122. switch (fullName) {
  123. case 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  124. t.pass(`expected search for registry value ${fullName}`)
  125. return cb(null, 'C:\\VS2015\\VC\\')
  126. case 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions\\14.0\\MSBuildToolsPath':
  127. t.pass(`expected search for registry value ${fullName}`)
  128. return cb(null, 'C:\\MSBuild14\\')
  129. default:
  130. t.fail(`unexpected search for registry value ${fullName}`)
  131. }
  132. }
  133. return cb(new Error())
  134. }
  135. finder.findVisualStudio()
  136. })
  137. test('error from PowerShell', function (t) {
  138. t.plan(2)
  139. const finder = new TestVisualStudioFinder(semverV1, null, null)
  140. finder.parseData(new Error(), '', '', (info) => {
  141. t.ok(/use PowerShell/i.test(finder.errorLog[0]), 'expect error')
  142. t.false(info, 'no data')
  143. })
  144. })
  145. test('empty output from PowerShell', function (t) {
  146. t.plan(2)
  147. const finder = new TestVisualStudioFinder(semverV1, null, null)
  148. finder.parseData(null, '', '', (info) => {
  149. t.ok(/use PowerShell/i.test(finder.errorLog[0]), 'expect error')
  150. t.false(info, 'no data')
  151. })
  152. })
  153. test('output from PowerShell not JSON', function (t) {
  154. t.plan(2)
  155. const finder = new TestVisualStudioFinder(semverV1, null, null)
  156. finder.parseData(null, 'AAAABBBB', '', (info) => {
  157. t.ok(/use PowerShell/i.test(finder.errorLog[0]), 'expect error')
  158. t.false(info, 'no data')
  159. })
  160. })
  161. test('wrong JSON from PowerShell', function (t) {
  162. t.plan(2)
  163. const finder = new TestVisualStudioFinder(semverV1, null, null)
  164. finder.parseData(null, '{}', '', (info) => {
  165. t.ok(/use PowerShell/i.test(finder.errorLog[0]), 'expect error')
  166. t.false(info, 'no data')
  167. })
  168. })
  169. test('empty JSON from PowerShell', function (t) {
  170. t.plan(2)
  171. const finder = new TestVisualStudioFinder(semverV1, null, null)
  172. finder.parseData(null, '[]', '', (info) => {
  173. t.ok(/find .* Visual Studio/i.test(finder.errorLog[0]), 'expect error')
  174. t.false(info, 'no data')
  175. })
  176. })
  177. test('future version', function (t) {
  178. t.plan(3)
  179. const finder = new TestVisualStudioFinder(semverV1, null, null)
  180. finder.parseData(null, JSON.stringify([{
  181. packages: [
  182. 'Microsoft.VisualStudio.Component.VC.Tools.x86.x64',
  183. 'Microsoft.VisualStudio.Component.Windows10SDK.17763',
  184. 'Microsoft.VisualStudio.VC.MSBuild.Base'
  185. ],
  186. path: 'C:\\VS',
  187. version: '9999.9999.9999.9999'
  188. }]), '', (info) => {
  189. t.ok(/unknown version/i.test(finder.errorLog[0]), 'expect error')
  190. t.ok(/find .* Visual Studio/i.test(finder.errorLog[1]), 'expect error')
  191. t.false(info, 'no data')
  192. })
  193. })
  194. test('single unusable VS2017', function (t) {
  195. t.plan(3)
  196. const finder = new TestVisualStudioFinder(semverV1, null, null)
  197. const file = path.join(__dirname, 'fixtures', 'VS_2017_Unusable.txt')
  198. const data = fs.readFileSync(file)
  199. finder.parseData(null, data, '', (info) => {
  200. t.ok(/checking/i.test(finder.errorLog[0]), 'expect error')
  201. t.ok(/find .* Visual Studio/i.test(finder.errorLog[2]), 'expect error')
  202. t.false(info, 'no data')
  203. })
  204. })
  205. test('minimal VS2017 Build Tools', function (t) {
  206. t.plan(2)
  207. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  208. t.strictEqual(err, null)
  209. t.deepEqual(info, {
  210. msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\' +
  211. 'BuildTools\\MSBuild\\15.0\\Bin\\MSBuild.exe',
  212. path:
  213. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\BuildTools',
  214. sdk: '10.0.17134.0',
  215. toolset: 'v141',
  216. version: '15.9.28307.665',
  217. versionMajor: 15,
  218. versionMinor: 9,
  219. versionYear: 2017
  220. })
  221. })
  222. poison(finder, 'regSearchKeys')
  223. finder.findVisualStudio2017OrNewer = (cb) => {
  224. const file = path.join(__dirname, 'fixtures',
  225. 'VS_2017_BuildTools_minimal.txt')
  226. const data = fs.readFileSync(file)
  227. finder.parseData(null, data, '', cb)
  228. }
  229. finder.findVisualStudio()
  230. })
  231. test('VS2017 Community with C++ workload', function (t) {
  232. t.plan(2)
  233. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  234. t.strictEqual(err, null)
  235. t.deepEqual(info, {
  236. msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\' +
  237. 'Community\\MSBuild\\15.0\\Bin\\MSBuild.exe',
  238. path:
  239. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community',
  240. sdk: '10.0.17763.0',
  241. toolset: 'v141',
  242. version: '15.9.28307.665',
  243. versionMajor: 15,
  244. versionMinor: 9,
  245. versionYear: 2017
  246. })
  247. })
  248. poison(finder, 'regSearchKeys')
  249. finder.findVisualStudio2017OrNewer = (cb) => {
  250. const file = path.join(__dirname, 'fixtures',
  251. 'VS_2017_Community_workload.txt')
  252. const data = fs.readFileSync(file)
  253. finder.parseData(null, data, '', cb)
  254. }
  255. finder.findVisualStudio()
  256. })
  257. test('VS2017 Express', function (t) {
  258. t.plan(2)
  259. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  260. t.strictEqual(err, null)
  261. t.deepEqual(info, {
  262. msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\' +
  263. 'WDExpress\\MSBuild\\15.0\\Bin\\MSBuild.exe',
  264. path:
  265. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\WDExpress',
  266. sdk: '10.0.17763.0',
  267. toolset: 'v141',
  268. version: '15.9.28307.858',
  269. versionMajor: 15,
  270. versionMinor: 9,
  271. versionYear: 2017
  272. })
  273. })
  274. poison(finder, 'regSearchKeys')
  275. finder.findVisualStudio2017OrNewer = (cb) => {
  276. const file = path.join(__dirname, 'fixtures', 'VS_2017_Express.txt')
  277. const data = fs.readFileSync(file)
  278. finder.parseData(null, data, '', cb)
  279. }
  280. finder.findVisualStudio()
  281. })
  282. test('VS2019 Preview with C++ workload', function (t) {
  283. t.plan(2)
  284. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  285. t.strictEqual(err, null)
  286. t.deepEqual(info, {
  287. msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\' +
  288. 'Preview\\MSBuild\\Current\\Bin\\MSBuild.exe',
  289. path:
  290. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Preview',
  291. sdk: '10.0.17763.0',
  292. toolset: 'v142',
  293. version: '16.0.28608.199',
  294. versionMajor: 16,
  295. versionMinor: 0,
  296. versionYear: 2019
  297. })
  298. })
  299. poison(finder, 'regSearchKeys')
  300. finder.findVisualStudio2017OrNewer = (cb) => {
  301. const file = path.join(__dirname, 'fixtures',
  302. 'VS_2019_Preview.txt')
  303. const data = fs.readFileSync(file)
  304. finder.parseData(null, data, '', cb)
  305. }
  306. finder.findVisualStudio()
  307. })
  308. test('minimal VS2019 Build Tools', function (t) {
  309. t.plan(2)
  310. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  311. t.strictEqual(err, null)
  312. t.deepEqual(info, {
  313. msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\' +
  314. 'BuildTools\\MSBuild\\Current\\Bin\\MSBuild.exe',
  315. path:
  316. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools',
  317. sdk: '10.0.17134.0',
  318. toolset: 'v142',
  319. version: '16.1.28922.388',
  320. versionMajor: 16,
  321. versionMinor: 1,
  322. versionYear: 2019
  323. })
  324. })
  325. poison(finder, 'regSearchKeys')
  326. finder.findVisualStudio2017OrNewer = (cb) => {
  327. const file = path.join(__dirname, 'fixtures',
  328. 'VS_2019_BuildTools_minimal.txt')
  329. const data = fs.readFileSync(file)
  330. finder.parseData(null, data, '', cb)
  331. }
  332. finder.findVisualStudio()
  333. })
  334. test('VS2019 Community with C++ workload', function (t) {
  335. t.plan(2)
  336. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  337. t.strictEqual(err, null)
  338. t.deepEqual(info, {
  339. msBuild: 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\' +
  340. 'Community\\MSBuild\\Current\\Bin\\MSBuild.exe',
  341. path:
  342. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Community',
  343. sdk: '10.0.17763.0',
  344. toolset: 'v142',
  345. version: '16.1.28922.388',
  346. versionMajor: 16,
  347. versionMinor: 1,
  348. versionYear: 2019
  349. })
  350. })
  351. poison(finder, 'regSearchKeys')
  352. finder.findVisualStudio2017OrNewer = (cb) => {
  353. const file = path.join(__dirname, 'fixtures',
  354. 'VS_2019_Community_workload.txt')
  355. const data = fs.readFileSync(file)
  356. finder.parseData(null, data, '', cb)
  357. }
  358. finder.findVisualStudio()
  359. })
  360. function allVsVersions (t, finder) {
  361. finder.findVisualStudio2017OrNewer = (cb) => {
  362. const data0 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  363. 'VS_2017_Unusable.txt')))
  364. const data1 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  365. 'VS_2017_BuildTools_minimal.txt')))
  366. const data2 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  367. 'VS_2017_Community_workload.txt')))
  368. const data3 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  369. 'VS_2017_Express.txt')))
  370. const data4 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  371. 'VS_2019_Preview.txt')))
  372. const data5 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  373. 'VS_2019_BuildTools_minimal.txt')))
  374. const data6 = JSON.parse(fs.readFileSync(path.join(__dirname, 'fixtures',
  375. 'VS_2019_Community_workload.txt')))
  376. const data = JSON.stringify(data0.concat(data1, data2, data3, data4,
  377. data5, data6))
  378. finder.parseData(null, data, '', cb)
  379. }
  380. finder.regSearchKeys = (keys, value, addOpts, cb) => {
  381. for (var i = 0; i < keys.length; ++i) {
  382. const fullName = `${keys[i]}\\${value}`
  383. switch (fullName) {
  384. case 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  385. case 'HKLM\\Software\\Microsoft\\VisualStudio\\SxS\\VC7\\12.0':
  386. continue
  387. case 'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7\\12.0':
  388. return cb(null, 'C:\\VS2013\\VC\\')
  389. case 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions\\12.0\\MSBuildToolsPath':
  390. return cb(null, 'C:\\MSBuild12\\')
  391. case 'HKLM\\Software\\Wow6432Node\\Microsoft\\VisualStudio\\SxS\\VC7\\14.0':
  392. return cb(null, 'C:\\VS2015\\VC\\')
  393. case 'HKLM\\Software\\Microsoft\\MSBuild\\ToolsVersions\\14.0\\MSBuildToolsPath':
  394. return cb(null, 'C:\\MSBuild14\\')
  395. default:
  396. t.fail(`unexpected search for registry value ${fullName}`)
  397. }
  398. }
  399. return cb(new Error())
  400. }
  401. }
  402. test('fail when looking for invalid path', function (t) {
  403. t.plan(2)
  404. const finder = new TestVisualStudioFinder(semverV1, 'AABB', (err, info) => {
  405. t.ok(/find .* Visual Studio/i.test(err), 'expect error')
  406. t.false(info, 'no data')
  407. })
  408. allVsVersions(t, finder)
  409. finder.findVisualStudio()
  410. })
  411. test('look for VS2013 by version number', function (t) {
  412. t.plan(2)
  413. const finder = new TestVisualStudioFinder(semverV1, '2013', (err, info) => {
  414. t.strictEqual(err, null)
  415. t.deepEqual(info.versionYear, 2013)
  416. })
  417. allVsVersions(t, finder)
  418. finder.findVisualStudio()
  419. })
  420. test('look for VS2013 by installation path', function (t) {
  421. t.plan(2)
  422. const finder = new TestVisualStudioFinder(semverV1, 'C:\\VS2013',
  423. (err, info) => {
  424. t.strictEqual(err, null)
  425. t.deepEqual(info.path, 'C:\\VS2013')
  426. })
  427. allVsVersions(t, finder)
  428. finder.findVisualStudio()
  429. })
  430. test('look for VS2015 by version number', function (t) {
  431. t.plan(2)
  432. const finder = new TestVisualStudioFinder(semverV1, '2015', (err, info) => {
  433. t.strictEqual(err, null)
  434. t.deepEqual(info.versionYear, 2015)
  435. })
  436. allVsVersions(t, finder)
  437. finder.findVisualStudio()
  438. })
  439. test('look for VS2015 by installation path', function (t) {
  440. t.plan(2)
  441. const finder = new TestVisualStudioFinder(semverV1, 'C:\\VS2015',
  442. (err, info) => {
  443. t.strictEqual(err, null)
  444. t.deepEqual(info.path, 'C:\\VS2015')
  445. })
  446. allVsVersions(t, finder)
  447. finder.findVisualStudio()
  448. })
  449. test('look for VS2017 by version number', function (t) {
  450. t.plan(2)
  451. const finder = new TestVisualStudioFinder(semverV1, '2017', (err, info) => {
  452. t.strictEqual(err, null)
  453. t.deepEqual(info.versionYear, 2017)
  454. })
  455. allVsVersions(t, finder)
  456. finder.findVisualStudio()
  457. })
  458. test('look for VS2017 by installation path', function (t) {
  459. t.plan(2)
  460. const finder = new TestVisualStudioFinder(semverV1,
  461. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community',
  462. (err, info) => {
  463. t.strictEqual(err, null)
  464. t.deepEqual(info.path,
  465. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community')
  466. })
  467. allVsVersions(t, finder)
  468. finder.findVisualStudio()
  469. })
  470. test('look for VS2019 by version number', function (t) {
  471. t.plan(2)
  472. const finder = new TestVisualStudioFinder(semverV1, '2019', (err, info) => {
  473. t.strictEqual(err, null)
  474. t.deepEqual(info.versionYear, 2019)
  475. })
  476. allVsVersions(t, finder)
  477. finder.findVisualStudio()
  478. })
  479. test('look for VS2019 by installation path', function (t) {
  480. t.plan(2)
  481. const finder = new TestVisualStudioFinder(semverV1,
  482. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools',
  483. (err, info) => {
  484. t.strictEqual(err, null)
  485. t.deepEqual(info.path,
  486. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools')
  487. })
  488. allVsVersions(t, finder)
  489. finder.findVisualStudio()
  490. })
  491. test('msvs_version match should be case insensitive', function (t) {
  492. t.plan(2)
  493. const finder = new TestVisualStudioFinder(semverV1,
  494. 'c:\\program files (x86)\\microsoft visual studio\\2019\\BUILDTOOLS',
  495. (err, info) => {
  496. t.strictEqual(err, null)
  497. t.deepEqual(info.path,
  498. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools')
  499. })
  500. allVsVersions(t, finder)
  501. finder.findVisualStudio()
  502. })
  503. test('latest version should be found by default', function (t) {
  504. t.plan(2)
  505. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  506. t.strictEqual(err, null)
  507. t.deepEqual(info.versionYear, 2019)
  508. })
  509. allVsVersions(t, finder)
  510. finder.findVisualStudio()
  511. })
  512. test('run on a usable VS Command Prompt', function (t) {
  513. t.plan(2)
  514. process.env.VCINSTALLDIR = 'C:\\VS2015\\VC'
  515. // VSINSTALLDIR is not defined on Visual C++ Build Tools 2015
  516. delete process.env.VSINSTALLDIR
  517. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  518. t.strictEqual(err, null)
  519. t.deepEqual(info.path, 'C:\\VS2015')
  520. })
  521. allVsVersions(t, finder)
  522. finder.findVisualStudio()
  523. })
  524. test('VCINSTALLDIR match should be case insensitive', function (t) {
  525. t.plan(2)
  526. process.env.VCINSTALLDIR =
  527. 'c:\\program files (x86)\\microsoft visual studio\\2019\\BUILDTOOLS\\VC'
  528. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  529. t.strictEqual(err, null)
  530. t.deepEqual(info.path,
  531. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools')
  532. })
  533. allVsVersions(t, finder)
  534. finder.findVisualStudio()
  535. })
  536. test('run on a unusable VS Command Prompt', function (t) {
  537. t.plan(2)
  538. process.env.VCINSTALLDIR =
  539. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildToolsUnusable\\VC'
  540. const finder = new TestVisualStudioFinder(semverV1, null, (err, info) => {
  541. t.ok(/find .* Visual Studio/i.test(err), 'expect error')
  542. t.false(info, 'no data')
  543. })
  544. allVsVersions(t, finder)
  545. finder.findVisualStudio()
  546. })
  547. test('run on a VS Command Prompt with matching msvs_version', function (t) {
  548. t.plan(2)
  549. process.env.VCINSTALLDIR = 'C:\\VS2015\\VC'
  550. const finder = new TestVisualStudioFinder(semverV1, 'C:\\VS2015',
  551. (err, info) => {
  552. t.strictEqual(err, null)
  553. t.deepEqual(info.path, 'C:\\VS2015')
  554. })
  555. allVsVersions(t, finder)
  556. finder.findVisualStudio()
  557. })
  558. test('run on a VS Command Prompt with mismatched msvs_version', function (t) {
  559. t.plan(2)
  560. process.env.VCINSTALLDIR =
  561. 'C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\BuildTools\\VC'
  562. const finder = new TestVisualStudioFinder(semverV1, 'C:\\VS2015',
  563. (err, info) => {
  564. t.ok(/find .* Visual Studio/i.test(err), 'expect error')
  565. t.false(info, 'no data')
  566. })
  567. allVsVersions(t, finder)
  568. finder.findVisualStudio()
  569. })