From cd9ad38a99c443ca576b1d52c394ee709051bb04 Mon Sep 17 00:00:00 2001 From: Steve Sanderson Date: Tue, 22 Aug 2017 16:13:34 -0700 Subject: [PATCH] Run tests against 'dotnet new' output instead of Yeoman output --- test/package.json | 3 +- test/templates/angular.spec.ts | 19 ++++++---- test/templates/util/dotnetnew.ts | 63 ++++++++++++++++++++++++++++++++ test/templates/util/yeoman.ts | 52 -------------------------- 4 files changed, 75 insertions(+), 62 deletions(-) create mode 100644 test/templates/util/dotnetnew.ts delete mode 100644 test/templates/util/yeoman.ts diff --git a/test/package.json b/test/package.json index 9bcf24a..39e4e4a 100644 --- a/test/package.json +++ b/test/package.json @@ -23,8 +23,7 @@ "selenium-standalone": "^5.9.0", "tree-kill": "^1.1.0", "typescript": "^2.1.4", - "webdriverio": "^4.5.0", - "yo": "^1.8.5" + "webdriverio": "^4.5.0" }, "devDependencies": { "wdio-junit-reporter": "^0.2.0", diff --git a/test/templates/angular.spec.ts b/test/templates/angular.spec.ts index 9a96d13..fc214e2 100644 --- a/test/templates/angular.spec.ts +++ b/test/templates/angular.spec.ts @@ -1,21 +1,24 @@ import * as fs from 'fs'; import * as path from 'path'; +import * as rimraf from 'rimraf'; import { expect } from 'chai'; -import { generateProjectSync } from './util/yeoman'; +import { generateProjectSync } from './util/dotnetnew'; import { AspNetProcess, AspNetCoreEnviroment, defaultUrl, publishProjectSync } from './util/aspnet'; import { getValue, getCssPropertyValue } from './util/webdriverio'; -// First, generate a new project using the locally-built generator-aspnetcore-spa +// First, generate a new project using the locally-built templates package // Do this outside the Mocha fixture, otherwise Mocha will time out const appDir = path.resolve(__dirname, '../generated/angular'); const publishedAppDir = path.resolve(appDir, './bin/Release/published'); +const publishedAppDllName = path.basename(appDir) + '.dll'; if (!process.env.SKIP_PROJECT_GENERATION) { - generateProjectSync(appDir, { - framework: 'angular', - name: 'Test App', - tests: true - }); + generateProjectSync(appDir, 'angular'); publishProjectSync(appDir, publishedAppDir); + + // Clear out any artifacts produced during publishing so they don't affect + // the dev-mode application + rimraf.sync(path.join(appDir, 'ClientApp/dist')); + rimraf.sync(path.join(appDir, 'wwwroot/dist')); } function testBasicNavigation() { @@ -95,6 +98,6 @@ describe('Angular template: dev mode', () => { }); describe('Angular template: production mode', () => { - AspNetProcess.RunInMochaContext(publishedAppDir, AspNetCoreEnviroment.production, 'TestApp.dll'); + AspNetProcess.RunInMochaContext(publishedAppDir, AspNetCoreEnviroment.production, publishedAppDllName); testBasicNavigation(); }); \ No newline at end of file diff --git a/test/templates/util/dotnetnew.ts b/test/templates/util/dotnetnew.ts new file mode 100644 index 0000000..171607a --- /dev/null +++ b/test/templates/util/dotnetnew.ts @@ -0,0 +1,63 @@ +import * as childProcess from 'child_process'; +import * as fs from 'fs'; +import * as path from 'path'; +import * as rimraf from 'rimraf'; +import * as mkdirp from 'mkdirp'; + +const templatePackageName = 'Microsoft.DotNet.Web.Spa.ProjectTemplates'; +const templatePackageArtifactsDir = '../templates/package-builder/dist/artifacts'; + +export function generateProjectSync(targetDir: string, templateName: string) { + installTemplatePackage(targetDir, templatePackageName, templateName); + executeDotNetNewTemplateSync(targetDir, templateName); + executeCommand('npm install', /* quiet */ false, targetDir); +} + +function installTemplatePackage(targetDir: string, packageName: string, templateName: string) { + // First figure out which file is the latest one for this package + const packagePaths = fs.readdirSync(templatePackageArtifactsDir) + .filter(name => name.startsWith(templatePackageName + '.')) + .filter(name => path.extname(name) === '.nupkg') + .map(name => path.join(templatePackageArtifactsDir, name)) + .sort(); + const latestPackagePath = packagePaths[packagePaths.length - 1]; + + if (!latestPackagePath) { + throw new Error(`Could not find ${packageName}.*.nupkg in directory ${templatePackageArtifactsDir}`); + } + + // Uninstall any older version so we can be sure the new one did install + try { + console.log(`Uninstalling any prior version of ${packageName}...`); + executeCommand(`dotnet new --uninstall ${packageName}`, /* quiet */ true); + } catch (ex) { + // Either no prior version existed, or we failed to uninstall. We'll determine + // which it was next. + } + try { + console.log(`Verifying that no prior version of ${packageName} is still installed...`); + executeDotNetNewTemplateSync(targetDir, templateName, /* quiet */ true); + throw new Error(`Failed to uninstall template package ${packageName}. The template '${templateName}' was not removed as expected.`); + } catch (ex) { + // Looks like we successfully uninstalled it + console.log(`Confirmed that no prior version of ${templatePackageName} remains installed.`); + } + + // Now install the new version + console.log(`Installing new templates package at ${latestPackagePath}...`); + executeCommand(`dotnet new --install ${latestPackagePath}`, /* quiet */ true); +} + +function executeDotNetNewTemplateSync(targetDir: string, templateName: string, quiet?: boolean) { + rimraf.sync(targetDir); + mkdirp.sync(targetDir); + executeCommand(`dotnet new ${templateName}`, quiet, targetDir); +} + +function executeCommand(command: string, quiet?: boolean, cwd?: string) { + childProcess.execSync(command, { + cwd, + stdio: quiet ? null : 'inherit', + encoding: 'utf8' + }); +} diff --git a/test/templates/util/yeoman.ts b/test/templates/util/yeoman.ts deleted file mode 100644 index 12177fc..0000000 --- a/test/templates/util/yeoman.ts +++ /dev/null @@ -1,52 +0,0 @@ -import * as childProcess from 'child_process'; -import * as path from 'path'; -import * as rimraf from 'rimraf'; -import * as mkdirp from 'mkdirp'; - -const generatorDirRelative = '../templates/package-builder/dist/generator-aspnetcore-spa'; -const yoPackageDirAbsolute = path.resolve('./node_modules/yo'); - -export interface GeneratorOptions { - framework: string; - name: string; - tests?: boolean; -} - -export function generateProjectSync(targetDir: string, generatorOptions: GeneratorOptions) { - const generatorDirAbsolute = path.resolve(generatorDirRelative); - console.log(`Running NPM install to prepare Yeoman generator at ${ generatorDirAbsolute }`); - childProcess.execSync(`npm install`, { stdio: 'inherit', cwd: generatorDirAbsolute }); - - console.log(`Ensuring empty output directory at ${ targetDir }`); - rimraf.sync(targetDir); - mkdirp.sync(targetDir); - - const yoExecutableAbsolute = findYeomanCliScript(); - console.log(`Will invoke Yeoman at ${ yoExecutableAbsolute } to generate application in ${ targetDir } with options:`); - console.log(JSON.stringify(generatorOptions, null, 2)); - const command = `node "${ yoExecutableAbsolute }" "${ path.resolve(generatorDirAbsolute, './app/index.js') }"`; - const args = makeYeomanCommandLineArgs(generatorOptions); - childProcess.execSync(`${ command } ${ args }`, { - stdio: 'inherit', - cwd: targetDir - }); -} - -function findYeomanCliScript() { - // On Windows, you can't invoke ./node_modules/.bin/yo from the shell for some reason. - // So instead, we'll locate the CLI entrypoint that yeoman would expose if it was installed globally. - const yeomanPackageJsonPath = path.join(yoPackageDirAbsolute, './package.json'); - const yeomanPackageJson = require(yeomanPackageJsonPath); - const yeomanCliScriptRelative = yeomanPackageJson.bin.yo; - if (!yeomanCliScriptRelative) { - throw new Error(`Could not find Yeoman CLI script. Looked for a bin/yo entry in ${ yeomanPackageJsonPath }`); - } - - return path.join(yoPackageDirAbsolute, yeomanCliScriptRelative); -} - -function makeYeomanCommandLineArgs(generatorOptions: GeneratorOptions) { - return Object.getOwnPropertyNames(generatorOptions) - .map(key => `--${ key }="${ generatorOptions[key] }"`) - .join(' '); -}