Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as sinon from 'sinon'
import { URI } from 'vscode-uri'
import { Logging, Workspace, WorkspaceFolder } from '@aws/language-server-runtimes/server-interface'
import { ArtifactManager } from './artifactManager'
import * as util from './util'

describe('ArtifactManager', () => {
const logging = {
log: () => {},
info: () => {},
warn: () => {},
error: () => {},
debug: () => {},
} as unknown as Logging
const proj: WorkspaceFolder = { uri: URI.file('/work/proj').toString(), name: 'proj' }
const projOther: WorkspaceFolder = { uri: URI.file('/work/proj-other').toString(), name: 'proj-other' }

let artifactManager: ArtifactManager
let processDirectory: sinon.SinonStub
let processFilesByLanguage: sinon.SinonStub

beforeEach(() => {
// Two workspace folders
artifactManager = new ArtifactManager({} as Workspace, logging, [proj, projOther])
// The directory processing reads the disk, so these tests check only the workspace folder that it gets
processDirectory = sinon.stub(artifactManager, 'processDirectory').resolves(new Map())
processFilesByLanguage = sinon.stub(artifactManager as any, 'processFilesByLanguage').resolves([])
})

afterEach(() => {
sinon.restore()
})

describe('addNewDirectories', () => {
it('processes a new directory with the workspace folder that the caller found', async () => {
await artifactManager.addNewDirectories([URI.file('/work/proj-other/src')], projOther)

sinon.assert.calledOnceWithExactly(processDirectory, projOther, '/work/proj-other/src', 'src')
// The files of the directory are zipped as part of the same folder, under the same relative path
sinon.assert.calledOnceWithExactly(
processFilesByLanguage,
projOther,
sinon.match.object,
sinon.match.map,
'src'
)
})

it('processes a new directory with a workspace folder whose URI has an uppercase drive letter', async () => {
const driveFolder: WorkspaceFolder = { uri: 'file:///C:/work/proj', name: 'proj' }

await artifactManager.addNewDirectories([URI.parse('file:///C:/work/proj/lib')], driveFolder)

sinon.assert.calledOnceWithExactly(processDirectory, driveFolder, '/C:/work/proj/lib', 'lib')
sinon.assert.calledOnceWithExactly(
processFilesByLanguage,
driveFolder,
sinon.match.object,
sinon.match.map,
'lib'
)
})
})

describe('handleRename', () => {
it('processes a renamed directory with the workspace folder of its new path', async () => {
sinon.stub(util, 'isDirectory').returns(true)
sinon.stub(util, 'isEmptyDirectory').returns(false)

await artifactManager.handleRename(
projOther,
URI.file('/work/proj-other/old').toString(),
URI.file('/work/proj-other/src').toString()
)

sinon.assert.calledOnceWithExactly(processDirectory, projOther, '/work/proj-other/src', 'src')
sinon.assert.calledOnceWithExactly(
processFilesByLanguage,
projOther,
sinon.match.object,
sinon.match.map,
'src'
)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ export class ArtifactManager {
return zipFileMetadata
}

async addNewDirectories(newDirectories: URI[]): Promise<FileMetadata[]> {
/**
* Processes the files of new directories as part of `workspaceFolder`, the workspace folder that the file event
* handler found for them
*/
async addNewDirectories(newDirectories: URI[], workspaceFolder: WorkspaceFolder): Promise<FileMetadata[]> {
let zipFileMetadata: FileMetadata[] = []
const fileSizeDetails: FileSizeDetails = {
includedFileCount: 0,
Expand All @@ -104,13 +108,6 @@ export class ArtifactManager {
}

for (const directory of newDirectories) {
const workspaceFolder = this.workspaceFolders.find(ws => directory.path.startsWith(URI.parse(ws.uri).path))

if (!workspaceFolder) {
// No workspace folder found for directory, it will not be processed
continue
}

try {
const workspacePath = URI.parse(workspaceFolder.uri).path
const relativePath = path.relative(workspacePath, directory.path)
Expand Down Expand Up @@ -317,7 +314,7 @@ export class ArtifactManager {
let filesMetadata: FileMetadata[] = []
if (isDirectory(newUri)) {
if (!isEmptyDirectory(newUri)) {
filesMetadata = await this.addNewDirectories([URI.parse(newUri)])
filesMetadata = await this.addNewDirectories([URI.parse(newUri)], workspaceFolder)
}
} else {
filesMetadata = [await this.processNewFile(workspaceFolder, newUri)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ export const WorkspaceContextServer = (): Server => features => {
if (isDir && isEmptyDirectory(file.uri)) {
continue
} else if (isDir) {
filesMetadata = await artifactManager.addNewDirectories([URI.parse(file.uri)])
filesMetadata = await artifactManager.addNewDirectories([URI.parse(file.uri)], workspaceFolder)
} else {
filesMetadata = [await artifactManager.processNewFile(workspaceFolder, file.uri)]
}
Expand Down
Loading