Skip to content
Open
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
11 changes: 11 additions & 0 deletions launcher/src/code-workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class CodeWorkspace {
return;
}

const projectsRoot = env.PROJECTS_ROOT;

let path: string | undefined;
let workspace: Workspace | undefined;

Expand Down Expand Up @@ -112,6 +114,15 @@ export class CodeWorkspace {
saveRequired = true;
}

// Ensure the workspace always opens at least the projects root.
// Empty workspaces (no projects, dependentProjects or starterProjects) would
// otherwise produce a `.code-workspace` file with no `folders`, causing the
// editor to open with an empty Explorer and nothing mounted.
if (!workspace!.folders || workspace!.folders.length === 0) {
workspace!.folders = [{ name: 'projects', path: projectsRoot }];
saveRequired = true;
}

// write workspace file only if it has been changed
if (saveRequired) {
const json = JSON.stringify(workspace, null, '\t');
Expand Down
20 changes: 20 additions & 0 deletions launcher/tests/_data/flattened.devworkspace.empty.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
attributes:
controller.devfile.io/devworkspace-config:
name: devworkspace-config
namespace: dogfooding
controller.devfile.io/storage-type: per-workspace
components:
- attributes:
app.kubernetes.io/component: che-code-runtime
app.kubernetes.io/part-of: che-code.eclipse.org
controller.devfile.io/merged-contributions: editor
container:
cpuLimit: "4"
image: quay.io/che-incubator/che-code-dev:insiders
memoryLimit: 13Gi
memoryRequest: 320Mi
sourceMapping: /projects
name: dev
- name: projects
volume:
size: 3Gi
46 changes: 46 additions & 0 deletions launcher/tests/code-workspace.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,15 @@ const WORKSPACE_WITH_FIVE_PROJECTS = `{
\t]
}`;

const WORKSPACE_WITH_PROJECTS_ROOT = `{
\t"folders": [
\t\t{
\t\t\t"name": "projects",
\t\t\t"path": "/tmp/projects"
\t\t}
\t]
}`;

const WORKSPACE_WITH_DEPENDENT_PROJECTS = `{
\t"folders": [
\t\t{
Expand Down Expand Up @@ -462,6 +471,43 @@ describe('Test generating VS Code Workspace file:', () => {
expect(writeFileMock).toBeCalledWith('/tmp/projects/.code-workspace', WORKSPACE_WITH_DEPENDENT_PROJECTS);
});

test('should add PROJECTS_ROOT as a default folder when the workspace has no projects', async () => {
env.PROJECTS_ROOT = '/tmp/projects';

env.DEVWORKSPACE_FLATTENED_DEVFILE = path.join(__dirname, '_data', 'flattened.devworkspace.empty.yaml');

const pathExistsMock = jest.fn();
const writeFileMock = jest.fn();
const readFileMock = jest.fn();

Object.assign(fs, {
pathExists: pathExistsMock,
writeFile: writeFileMock,
readFile: readFileMock,
});

readFileMock.mockImplementation(async (path) => {
if (path === env.DEVWORKSPACE_FLATTENED_DEVFILE) {
return originalReadFile(path);
}

return undefined;
});

// no project directories exist and no default .code-workspace file is present
pathExistsMock.mockImplementation(async () => false);

const codeWorkspace = new CodeWorkspace();
await codeWorkspace.generate();

// should read only the flattened devworkspace file
expect(readFileMock).toBeCalledTimes(1);
expect(readFileMock).toBeCalledWith(env.DEVWORKSPACE_FLATTENED_DEVFILE);

// should create a default workspace file that opens the projects root
expect(writeFileMock).toBeCalledWith('/tmp/projects/.code-workspace', WORKSPACE_WITH_PROJECTS_ROOT);
});

test('should parse .code-workspace file if the file has extra characters', async () => {
env.PROJECTS_ROOT = '/tmp/projects';

Expand Down