Skip to content

Commit

Permalink
Fixing flapping tests.
Browse files Browse the repository at this point in the history
Looks like the async() approach was causing a sporadic failure, due to askUserToOverwriteLandingPage() not being stubbed.
  • Loading branch information
dbreese committed Feb 13, 2024
1 parent 04eb0e5 commit f0ba149
Showing 1 changed file with 91 additions and 100 deletions.
191 changes: 91 additions & 100 deletions src/test/suite/commands/wizard/templateChooserCommand.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,20 @@ type LandingPageTestIOConfig = {
};

suite('Template Chooser Command Test Suite', () => {
beforeEach(function () {});
let sandbox: sinon.SinonSandbox;

beforeEach(function () {
sandbox = sinon.createSandbox();
});

afterEach(function () {
sinon.restore();
sandbox.restore();
});

test('Landing pages exist: existing landing page file combinations', async () => {
const projectDirMgr =
await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getWorkspaceDir'
);
Expand All @@ -46,63 +50,53 @@ suite('Template Chooser Command Test Suite', () => {
);
await mkdir(staticResourcesAbsPath, { recursive: true });

(async () => {
for (const lptIndex in TemplateChooserCommand.LANDING_PAGE_FILENAME_PREFIXES) {
const landingPageType = lptIndex as LandingPageType;
const fileConfigList: LandingPageTestIOConfig[] = [
{
[landingPageType]: {
jsonExists: false,
metaExists: false
}
},
{
[landingPageType]: {
jsonExists: true,
metaExists: false
}
},
{
[landingPageType]: {
jsonExists: false,
metaExists: true
}
},
{
[landingPageType]: {
jsonExists: true,
metaExists: true
}
for (const lptIndex in TemplateChooserCommand.LANDING_PAGE_FILENAME_PREFIXES) {
const landingPageType = lptIndex as LandingPageType;
const fileConfigList: LandingPageTestIOConfig[] = [
{
[landingPageType]: {
jsonExists: false,
metaExists: false
}
},
{
[landingPageType]: {
jsonExists: true,
metaExists: false
}
},
{
[landingPageType]: {
jsonExists: false,
metaExists: true
}
},
{
[landingPageType]: {
jsonExists: true,
metaExists: true
}
];
for (const fileConfig of fileConfigList) {
createLandingPageContent(
fileConfig,
staticResourcesAbsPath
);
const filesExist =
await TemplateChooserCommand.landingPageFilesExist(
staticResourcesAbsPath,
landingPageType
);
assert.equal(
filesExist.jsonFileExists,
fileConfig[landingPageType]!.jsonExists
);
assert.equal(
filesExist.metaFileExists,
fileConfig[landingPageType]!.metaExists
);
deleteLandingPageContent(
fileConfig,
staticResourcesAbsPath
);
}
];
for (const fileConfig of fileConfigList) {
createLandingPageContent(fileConfig, staticResourcesAbsPath);
const filesExist =
await TemplateChooserCommand.landingPageFilesExist(
staticResourcesAbsPath,
landingPageType
);
assert.equal(
filesExist.jsonFileExists,
fileConfig[landingPageType]!.jsonExists
);
assert.equal(
filesExist.metaFileExists,
fileConfig[landingPageType]!.metaExists
);
deleteLandingPageContent(fileConfig, staticResourcesAbsPath);
}
})().then(async () => {
await projectDirMgr.removeDir();
getWorkspaceDirStub.restore();
});
}
await projectDirMgr.removeDir();
});

test('Choosing existing landing page automatically resolves', async () => {
Expand All @@ -115,7 +109,7 @@ suite('Template Chooser Command Test Suite', () => {
test('User is asked to overwrite existing landing page and dialog is cancelled', async () => {
const projectDirMgr =
await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getWorkspaceDir'
);
Expand All @@ -133,7 +127,7 @@ suite('Template Chooser Command Test Suite', () => {
};
createLandingPageContent(config, staticResourcesAbsPath);

const askUserToOverwriteStub = sinon.stub(
const askUserToOverwriteStub = sandbox.stub(
TemplateChooserCommand,
'askUserToOverwriteLandingPage'
);
Expand All @@ -158,16 +152,13 @@ suite('Template Chooser Command Test Suite', () => {
'Choice was to cancel the dialog for overwriting existing page.'
);

askUserToOverwriteStub.restore();
await projectDirMgr.removeDir();
getWorkspaceDirStub.restore();
});


test('User is asked to overwrite existing landing page', async () => {
const projectDirMgr =
await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getWorkspaceDir'
);
Expand All @@ -185,7 +176,7 @@ suite('Template Chooser Command Test Suite', () => {
};
createLandingPageContent(config, staticResourcesAbsPath);

const askUserToOverwriteStub = sinon.stub(
const askUserToOverwriteStub = sandbox.stub(
TemplateChooserCommand,
'askUserToOverwriteLandingPage'
);
Expand All @@ -210,19 +201,24 @@ suite('Template Chooser Command Test Suite', () => {
'Choice was not to overwrite existing page.'
);

askUserToOverwriteStub.restore();
await projectDirMgr.removeDir();
getWorkspaceDirStub.restore();
});

test('Landing page template written to landing page files', async () => {
const projectDirMgr =
await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getWorkspaceDir'
);
getWorkspaceDirStub.returns(projectDirMgr.projectDir);

const confirmationDialogStub = sandbox.stub(
TemplateChooserCommand,
'askUserToOverwriteLandingPage'
);
confirmationDialogStub.resolves('Yes');

const staticResourcesAbsPath = path.join(
projectDirMgr.projectDir,
WorkspaceUtils.STATIC_RESOURCES_PATH
Expand All @@ -249,40 +245,36 @@ suite('Template Chooser Command Test Suite', () => {
};
createLandingPageContent(landingPageIoConfig, staticResourcesAbsPath);

(async () => {
for (const lptIndex in TemplateChooserCommand.LANDING_PAGE_FILENAME_PREFIXES) {
const landingPageType = lptIndex as LandingPageType;
if (landingPageType === 'existing') {
continue;
}
const copied = await TemplateChooserCommand.onLandingPageChosen(
{ landingPageType }
for (const lptIndex in TemplateChooserCommand.LANDING_PAGE_FILENAME_PREFIXES) {
const landingPageType = lptIndex as LandingPageType;
if (landingPageType === 'existing') {
continue;
}
const copied = await TemplateChooserCommand.onLandingPageChosen({
landingPageType
});
assert.ok(
copied,
`Landing page for type '${landingPageType}' should have been copied.`
);
for (const landingPageExtension of [
TemplateChooserCommand.LANDING_PAGE_JSON_FILE_EXTENSION,
TemplateChooserCommand.LANDING_PAGE_METADATA_FILE_EXTENSION
]) {
const fileName =
TemplateChooserCommand.LANDING_PAGE_FILENAME_PREFIXES
.existing + landingPageExtension;
const readContent = fs.readFileSync(
path.join(staticResourcesAbsPath, fileName),
{ encoding: 'utf-8' }
);
assert.ok(
copied,
`Landing page for type '${landingPageType}' should have been copied.`
assert.equal(
readContent,
`${landingPageType} ${landingPageExtension} content`
);
for (const landingPageExtension of [
TemplateChooserCommand.LANDING_PAGE_JSON_FILE_EXTENSION,
TemplateChooserCommand.LANDING_PAGE_METADATA_FILE_EXTENSION
]) {
const fileName =
TemplateChooserCommand.LANDING_PAGE_FILENAME_PREFIXES
.existing + landingPageExtension;
const readContent = fs.readFileSync(
path.join(staticResourcesAbsPath, fileName),
{ encoding: 'utf-8' }
);
assert.equal(
readContent,
`${landingPageType} ${landingPageExtension} content`
);
}
}
})().then(async () => {
await projectDirMgr.removeDir();
getWorkspaceDirStub.restore();
});
}
await projectDirMgr.removeDir();
});

test('Landing page status: staticresources does not exist', async () => {
Expand All @@ -293,7 +285,7 @@ suite('Template Chooser Command Test Suite', () => {
test('Landing page status: various file existence scenarios', async () => {
const projectDirMgr =
await TempProjectDirManager.createTempProjectDir();
const getWorkspaceDirStub = sinon.stub(
const getWorkspaceDirStub = sandbox.stub(
WorkspaceUtils,
'getWorkspaceDir'
);
Expand Down Expand Up @@ -348,7 +340,6 @@ suite('Template Chooser Command Test Suite', () => {
}

await projectDirMgr.removeDir();
getWorkspaceDirStub.restore();
});
});

Expand Down

0 comments on commit f0ba149

Please sign in to comment.