Skip to content
Merged
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
49 changes: 44 additions & 5 deletions packages/playground/blueprints/src/lib/v1/resources.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,6 @@ describe('GitDirectoryResource', () => {

it('defaults to the repo root when path is omitted', async () => {
const url = 'https://github.com/WordPress/wordpress-playground';
const fallbackName = url
.replaceAll(/[^a-zA-Z0-9-.]/g, '-')
.replaceAll(/-+/g, '-');
const resource = new GitDirectoryResource({
resource: 'git:directory',
url,
Expand All @@ -67,11 +64,53 @@ describe('GitDirectoryResource', () => {
});
const { files, name } = await resource.resolve();

expect(name).toBe(fallbackName);
expect(resource.name).toBe('.github');
// Human-readable name
expect(resource.name).toBe(
'https://github.com/WordPress/wordpress-playground (trunk) at .github'
);

// Filename
expect(name).toBe(
'https-github.com-WordPress-wordpress-playground-trunk-at-.github'
);
expect(files['dependabot.yml']).toBeInstanceOf(Uint8Array);
});
});

describe('name', () => {
it('should return a non-empty name when path is omitted', async () => {
const resource = new GitDirectoryResource({
resource: 'git:directory',
url: 'https://github.com/WordPress/link-manager',
ref: 'trunk',
});
const { name } = await resource.resolve();
expect(name).toBe('https-github.com-WordPress-link-manager-trunk');
});

it('should return a non-empty name when path is empty', async () => {
const resource = new GitDirectoryResource({
resource: 'git:directory',
url: 'https://github.com/WordPress/link-manager',
ref: 'trunk',
path: '',
});
const { name } = await resource.resolve();
expect(name).toBe('https-github.com-WordPress-link-manager-trunk');
});

it('should return a non-empty name when path has no letters', async () => {
const resource = new GitDirectoryResource({
resource: 'git:directory',
url: 'https://github.com/WordPress/link-manager',
ref: 'trunk',
// A path with only a few files to avoid timing out.
path: '/',
});
const { name } = await resource.resolve();
expect(name).toBe('https-github.com-WordPress-link-manager-trunk');
});
});
});

describe('BlueprintResource', () => {
Expand Down
37 changes: 24 additions & 13 deletions packages/playground/blueprints/src/lib/v1/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from '@php-wasm/progress';
import type { FileTree, UniversalPHP } from '@php-wasm/universal';
import type { Semaphore } from '@php-wasm/util';
import { dirname } from '@php-wasm/util';
import { randomFilename } from '@php-wasm/util';
import {
listDescendantFiles,
listGitFiles,
Expand Down Expand Up @@ -586,24 +586,35 @@ export class GitDirectoryResource extends Resource<Directory> {
name.substring(requestedPath.length).replace(/^\/+/, '')
);
return {
name:
dirname(this.reference.path || '') ||
this.reference.url
.replaceAll(/[^a-zA-Z0-9-.]/g, '-')
.replaceAll(/-+/g, '-'),
name: this.filename,
files,
};
}

/**
* Generate a nice, non-empty filename – the installPlugin step depends on it.
*/
get filename() {
return (
this.name
.replaceAll(/[^a-zA-Z0-9-.]/g, '-')
.replaceAll(/-+/g, '-')
.replace(/^[^a-zA-Z0-9]+|[^a-zA-Z0-9]+$/g, '') ||
randomFilename()
);
}

/** @inheritDoc */
get name() {
const path = this.reference.path ?? '';
if (!path) {
return this.reference.url
.replaceAll(/[^a-zA-Z0-9-.]/g, '-')
.replaceAll(/-+/g, '-');
}
return path.split('/').pop() || '';
return [
this.reference.url,
this.reference.ref ? `(${this.reference.ref})` : '',
this.reference.path?.replace(/^\/+/, '')
? `at ${this.reference.path}`
: '',
]
.filter((segment) => segment.length > 0)
.join(' ');
}
}

Expand Down