Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for absolute paths in srcDir and specDir #50

Merged
merged 3 commits into from
Mar 13, 2024
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 16 additions & 8 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,12 @@ class Server {
}

getUrls(baseDir, globs, urlRoot) {
return findFiles(path.join(this.projectBaseDir, baseDir), globs || []).map(
function(p) {
return isUrl(p) ? p : unWindows(path.join(urlRoot, p));
}
);
return findFiles(
path.resolve(this.projectBaseDir, baseDir),
globs || []
).map(function(p) {
return isUrl(p) ? p : unWindows(path.join(urlRoot, p));
});
}

getSupportFiles() {
Expand Down Expand Up @@ -148,11 +149,15 @@ class Server {
);
app.use(
'/__spec__',
this.express.static(path.join(this.projectBaseDir, this.options.specDir))
this.express.static(
path.resolve(this.projectBaseDir, this.options.specDir)
)
);
app.use(
'/__src__',
this.express.static(path.join(this.projectBaseDir, this.options.srcDir))
this.express.static(
path.resolve(this.projectBaseDir, this.options.srcDir)
)
);

if (this.options.middleware) {
Expand All @@ -165,7 +170,10 @@ class Server {

if (this.options.importMap) {
const dir = this.options.importMap.moduleRootDir
? path.join(this.projectBaseDir, this.options.importMap.moduleRootDir)
? path.resolve(
this.projectBaseDir,
this.options.importMap.moduleRootDir
)
: this.projectBaseDir;
app.use('/__moduleRoot__', this.express.static(dir));
}
Expand Down
25 changes: 24 additions & 1 deletion spec/serverSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,29 @@ describe('server', function() {
expect(server.projectBaseDir).toEqual(path.resolve());
});

it('allows absolute paths for srcDir and specDir', async function() {
const projectDir = path.resolve(__dirname, 'fixtures/sampleProject');
const srcDir = path.resolve(projectDir, 'sources');
const specDir = path.resolve(projectDir, 'specs');
const server = new Server({
srcDir,
specDir,
srcFiles: ['thing2.js', '**/*.js'],
helpers: ['helpers/**/*.js'],
specFiles: ['**/*[sS]pec.js'],
});

await server.start({ port: 0 });

const baseUrl = `http://localhost:${server.port()}`;

// Even though the base dir isn't set on the server, we can still get the
// source file through the absolute path.
expect(server.projectBaseDir).toEqual(path.resolve());
expect(server.projectBaseDir).not.toEqual(projectDir);
await getFile(baseUrl + '/__src__/thing2.js');
});

it('appends specified css files after Jasmines own', function() {
const server = new Server({
projectBaseDir: path.resolve(__dirname, 'fixtures/sampleProject'),
Expand Down Expand Up @@ -574,7 +597,7 @@ describe('server', function() {
},
});

await server.start();
await server.start({ port: 0 });

expect(app.use).toHaveBeenCalledWith('/foo', middleware1);
expect(app.use).toHaveBeenCalledWith('/bar', middleware2);
Expand Down