|
| 1 | +/*! |
| 2 | + * Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +import * as assert from 'assert' |
| 7 | +import * as vscode from 'vscode' |
| 8 | +import { getDevfileLocation } from '../../codecatalyst/model' |
| 9 | +import { DevEnvClient } from '../../shared/clients/devenvClient' |
| 10 | +import * as sinon from 'sinon' |
| 11 | +const fileSystemUtils = require('../../shared/filesystemUtilities') |
| 12 | + |
| 13 | +describe('getDevfileLocation', function () { |
| 14 | + let sandbox: sinon.SinonSandbox |
| 15 | + |
| 16 | + beforeEach(function () { |
| 17 | + sandbox = sinon.createSandbox() |
| 18 | + }) |
| 19 | + |
| 20 | + afterEach(function () { |
| 21 | + sandbox.restore() |
| 22 | + }) |
| 23 | + |
| 24 | + function mockClient(location: string | undefined): DevEnvClient { |
| 25 | + const devEnvClient = new DevEnvClient() |
| 26 | + if (!location) { |
| 27 | + sandbox.stub(devEnvClient, 'getStatus').resolves({}) |
| 28 | + } else { |
| 29 | + sandbox.stub(devEnvClient, 'getStatus').resolves({ |
| 30 | + location: location, |
| 31 | + }) |
| 32 | + } |
| 33 | + return devEnvClient |
| 34 | + } |
| 35 | + |
| 36 | + it('devfile found at root', async function () { |
| 37 | + const client = mockClient('devfile.yaml') |
| 38 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects')) |
| 39 | + assert.strictEqual(location.toString(), 'file:///projects/devfile.yaml') |
| 40 | + }) |
| 41 | + |
| 42 | + it('devfile with repo found in subfolder', async function () { |
| 43 | + const client = mockClient('WebApplication/devfile.yaml') |
| 44 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects')) |
| 45 | + assert.strictEqual(location.toString(), 'file:///projects/WebApplication/devfile.yaml') |
| 46 | + }) |
| 47 | + |
| 48 | + it('devfile without repo found in workspace root', async function () { |
| 49 | + const devfilePath = vscode.Uri.parse('/projects/WebApplication/devfile.yaml').fsPath |
| 50 | + sandbox.stub(fileSystemUtils, 'fileExists').callsFake(function (p: string) { |
| 51 | + return p === devfilePath |
| 52 | + }) |
| 53 | + const client = mockClient('devfile.yaml') |
| 54 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects/WebApplication')) |
| 55 | + assert.strictEqual(location.toString(), 'file:///projects/WebApplication/devfile.yaml') |
| 56 | + }) |
| 57 | + |
| 58 | + it('devfile found in subfolder with repo', async function () { |
| 59 | + const devfilePath = vscode.Uri.parse('/projects/WebApplication/devfile.yaml').fsPath |
| 60 | + sandbox.stub(fileSystemUtils, 'fileExists').callsFake(function (p: string) { |
| 61 | + return p === devfilePath |
| 62 | + }) |
| 63 | + const client = mockClient('WebApplication/devfile.yaml') |
| 64 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects/WebApplication')) |
| 65 | + assert.strictEqual(location.toString(), 'file:///projects/WebApplication/devfile.yaml') |
| 66 | + }) |
| 67 | + |
| 68 | + it('throws when devfile is not found', async function () { |
| 69 | + sandbox.stub(fileSystemUtils, 'fileExists').resolves(false) |
| 70 | + const client = mockClient('test/devfile.yaml') |
| 71 | + const location = getDevfileLocation(client, vscode.Uri.parse('/projects/WebApplication')) |
| 72 | + assert.rejects(location, new Error('Devfile location was not found')) |
| 73 | + }) |
| 74 | + |
| 75 | + it('falls back to default projects location when devfile cannot be located', async function () { |
| 76 | + const devfilePath = vscode.Uri.parse('/projects/devfile.yaml').fsPath |
| 77 | + sandbox.stub(fileSystemUtils, 'fileExists').callsFake(function (p: string) { |
| 78 | + return p === devfilePath |
| 79 | + }) |
| 80 | + const client = mockClient('WebApplication/devfile.yaml') |
| 81 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects/WebApplication')) |
| 82 | + assert.strictEqual(location.toString(), 'file:///projects/devfile.yaml') |
| 83 | + }) |
| 84 | + |
| 85 | + it('falls back to default workspace location when devfile cannot be located', async function () { |
| 86 | + const devfilePath = vscode.Uri.parse('/projects/WebApplication/devfile.yaml').fsPath |
| 87 | + sandbox.stub(fileSystemUtils, 'fileExists').callsFake(function (p: string) { |
| 88 | + return p === devfilePath |
| 89 | + }) |
| 90 | + const client = mockClient('devfile.yaml') |
| 91 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects/WebApplication')) |
| 92 | + assert.strictEqual(location.toString(), 'file:///projects/WebApplication/devfile.yaml') |
| 93 | + }) |
| 94 | + |
| 95 | + it('checks project root for devfile when location isnt specified', async function () { |
| 96 | + const devfilePath = vscode.Uri.parse('/projects/devfile.yaml').fsPath |
| 97 | + sandbox.stub(fileSystemUtils, 'fileExists').callsFake(function (p: string) { |
| 98 | + return p === devfilePath |
| 99 | + }) |
| 100 | + const client = mockClient(undefined) |
| 101 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects')) |
| 102 | + assert.strictEqual(location.toString(), 'file:///projects/devfile.yaml') |
| 103 | + }) |
| 104 | + |
| 105 | + it('checks workspace root for devfile when location isnt specified', async function () { |
| 106 | + const devfilePath = vscode.Uri.parse('/projects/WebApplication/devfile.yaml').fsPath |
| 107 | + sandbox.stub(fileSystemUtils, 'fileExists').callsFake(function (p: string) { |
| 108 | + return p === devfilePath |
| 109 | + }) |
| 110 | + const client = mockClient(undefined) |
| 111 | + const location = await getDevfileLocation(client, vscode.Uri.parse('/projects/WebApplication')) |
| 112 | + assert.strictEqual(location.toString(), 'file:///projects/WebApplication/devfile.yaml') |
| 113 | + }) |
| 114 | +}) |
0 commit comments