|
| 1 | +/*! |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | +import * as vscode from 'vscode' |
| 6 | +import { TestFolder } from '../../testUtil' |
| 7 | +import { detectSamProjects, getFiles } from '../../../awsService/appBuilder/explorer/detectSamProjects' |
| 8 | +import assert from 'assert' |
| 9 | +import * as sinon from 'sinon' |
| 10 | + |
| 11 | +import path from 'path' |
| 12 | +import { ToolkitError } from '../../../shared' |
| 13 | +import { assertLogsContain } from '../../globalSetup.test' |
| 14 | + |
| 15 | +describe('detectSamProjects', () => { |
| 16 | + let sandbox: sinon.SinonSandbox |
| 17 | + |
| 18 | + beforeEach(async () => { |
| 19 | + sandbox = sinon.createSandbox() |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(async () => { |
| 23 | + sandbox.restore() |
| 24 | + }) |
| 25 | + |
| 26 | + it('should return an empty array when no project found', async () => { |
| 27 | + const testFolder = await TestFolder.create() |
| 28 | + sandbox.stub(vscode.workspace, 'workspaceFolders').value([ |
| 29 | + { |
| 30 | + index: 0, |
| 31 | + name: 'test-workspace-folder', |
| 32 | + uri: testFolder, |
| 33 | + }, |
| 34 | + ]) |
| 35 | + const projects = await detectSamProjects() |
| 36 | + assert.strictEqual(projects.length, 0) |
| 37 | + }) |
| 38 | + |
| 39 | + it('should return an empty array when (unlikely) workspace folders is undefined', async () => { |
| 40 | + sandbox.stub(vscode.workspace, 'workspaceFolders').value(undefined) |
| 41 | + const projects = await detectSamProjects() |
| 42 | + assert.strictEqual(projects.length, 0) |
| 43 | + }) |
| 44 | + |
| 45 | + it('should return an non empty array and test projects in testFixture', async () => { |
| 46 | + const projects = await detectSamProjects() |
| 47 | + assert(projects.length >= 20) |
| 48 | + assert( |
| 49 | + projects.some((p) => { |
| 50 | + const projectRootName = path.relative(p.workspaceFolder.uri.fsPath, p.projectRoot.fsPath) |
| 51 | + return projectRootName === 'appbuilder-test-app' |
| 52 | + }) |
| 53 | + ) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe('getFiles', () => { |
| 58 | + let workspaceFolder: vscode.WorkspaceFolder |
| 59 | + |
| 60 | + beforeEach(() => { |
| 61 | + const workspaceFolders = vscode.workspace.workspaceFolders |
| 62 | + assert(workspaceFolders) |
| 63 | + workspaceFolder = workspaceFolders[0] |
| 64 | + }) |
| 65 | + |
| 66 | + it('should return an array of one project folder matching pattern without excluded pattern', async () => { |
| 67 | + const templateFiles = await getFiles(workspaceFolder, '**/appbuilder-test-app/template.{yml,yaml}') |
| 68 | + assert.strictEqual(templateFiles.length, 1) |
| 69 | + assert(templateFiles[0].fsPath.endsWith(path.join('appbuilder-test-app', 'template.yaml'))) |
| 70 | + }) |
| 71 | + |
| 72 | + it('should return a non empty array contains all project except the excluded pattern', async () => { |
| 73 | + const allTemplateFiles = await getFiles(workspaceFolder, '**/template.{yml,yaml}') |
| 74 | + const templateFiles = await getFiles(workspaceFolder, '**/template.{yml,yaml}', '**/appbuilder-test-app/**') |
| 75 | + |
| 76 | + assert.strictEqual(allTemplateFiles.length - templateFiles.length, 1) |
| 77 | + assert(!templateFiles.some((f) => f.fsPath.endsWith(path.join('appbuilder-test-app', 'template.yaml')))) |
| 78 | + }) |
| 79 | + |
| 80 | + it('should return empty array given any error', async () => { |
| 81 | + const sandbox = sinon.createSandbox() |
| 82 | + sandbox.stub(vscode.workspace, 'findFiles').rejects(new ToolkitError('mock an unlikely error')) |
| 83 | + |
| 84 | + const templateFiles = await getFiles(workspaceFolder, '**/template.{yml,yaml}', '**/.aws-sam/**') |
| 85 | + assert.strictEqual(templateFiles.length, 0) |
| 86 | + assertLogsContain('Failed to get files with pattern', false, 'error') |
| 87 | + sandbox.restore() |
| 88 | + }) |
| 89 | +}) |
0 commit comments