Skip to content

(test): fixing tests node v18 #118

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

Closed
Closed
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
3 changes: 0 additions & 3 deletions .eslintignore

This file was deleted.

13 changes: 0 additions & 13 deletions .eslintrc.yml

This file was deleted.

4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:

jobs:
test:
runs-on: ubuntu-20.04
runs-on: ubuntu-latest
strategy:
matrix:
name:
Expand Down Expand Up @@ -222,4 +222,4 @@ jobs:
uses: coverallsapp/github-action@master
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
parallel-finished: true
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/). Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally):

```sh
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bjohansebas linter continued marking me this line as error. I could not find a workaround

```
$ npm install serve-index
```

Expand Down
44 changes: 44 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { defineConfig, globalIgnores } from "eslint/config";
import markdown from "eslint-plugin-markdown";
import path from "node:path";
import { fileURLToPath } from "node:url";
import js from "@eslint/js";
import { FlatCompat } from "@eslint/eslintrc";

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const compat = new FlatCompat({
baseDirectory: __dirname,
recommendedConfig: js.configs.recommended,
allConfig: js.configs.all
});

export default defineConfig([globalIgnores(["**/.nyc_output", "**/coverage", "**/node_modules"]), {
extends: compat.extends("plugin:markdown/recommended"),

plugins: {
markdown,
},

rules: {
"eol-last": "error",
eqeqeq: ["error", "allow-null"],

indent: ["error", 2, {
SwitchCase: 1,
}],

"no-trailing-spaces": "error",
},
ignores: [".nyc_output", "coverage", "node_modules"]
}, {
files: ["**/*.md"],
processor: "markdown/markdown",
}, {
files: ["**/*.md/*.js"],
plugins: {
markdown
},
extends: compat.extends("plugin:markdown/recommended")
}
]);
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
"parseurl": "~1.3.3"
},
"devDependencies": {
"@eslint/eslintrc": "^3.3.1",
"@eslint/js": "^9.27.0",
"after": "0.8.2",
"eslint": "7.23.0",
"eslint": "^9.27.0",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recommend not updating the linter yet, there's a broader discussion around it (see expressjs/discussions#327)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. I'll revert it

"eslint-plugin-markdown": "2.0.0",
"mocha": "10.4.0",
"nyc": "15.1.0",
Expand All @@ -33,8 +35,9 @@
},
"scripts": {
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "mocha --reporter spec --bail --check-leaks test/",
"test-ci": "nyc --reporter=lcov --reporter=text npm test",
"test-cov": "nyc --reporter=html --reporter=text npm test"
}
}
}
73 changes: 54 additions & 19 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ var fixtures = path.join(__dirname, '/fixtures');
var relative = path.relative(process.cwd(), fixtures);

var skipRelative = ~relative.indexOf('..') || path.resolve(relative) === relative;
var NODE_BASE_VERSION = 16;
var major = process.versions.node.split('.')?.map(Number)?.[0] || NODE_BASE_VERSION;

describe('serveIndex(root)', function () {
it('should require root', function () {
Expand Down Expand Up @@ -86,13 +88,24 @@ describe('serveIndex(root)', function () {
.expect(400, done)
})

it('should deny path outside root', function (done) {
var server = createServer()
if (major < NODE_BASE_VERSION) {
/**
* For Node versions lower than 10. To keep retrocompatibility
*
* Inside serveIndex function, it uses path.normalize()
* When it receives the path '/../' the normalize function resolves it to '/'
* Therefore, this test will pass
*
* @see https://nodejs.org/download/release/v18.16.0/docs/api/path.html#pathnormalizepath
*/
it('should deny path outside root', function (done) {
var server = createServer()

request(server)
.get('/../')
.expect(403, done)
})
request(server)
.get('/../')
.expect(403, done)
})
}

it('should skip non-existent paths', function (done) {
var server = createServer()
Expand Down Expand Up @@ -742,14 +755,25 @@ describe('serveIndex(root)', function () {
.end(done)
});

it('should not work for outside root', function (done) {
var server = createServer()
if (major < NODE_BASE_VERSION) {
/**
* For Node versions lower than 10. To keep retrocompatibility
*
* Inside serveIndex function, it uses path.normalize()
* When it receives the path '/../support/' the normalize function resolves it to '/support/'
* Therefore, this test will pass
*
* @see https://nodejs.org/download/release/v18.16.0/docs/api/path.html#pathnormalizepath
*/
it('should not work for outside root', function (done) {
var server = createServer()

request(server)
.get('/../support/')
.set('Accept', 'text/html')
.expect(403, done)
});
request(server)
.get('/../support/')
.set('Accept', 'text/html')
.expect(403, done)
});
}
});

describe('when setting a custom stylesheet', function () {
Expand Down Expand Up @@ -807,12 +831,23 @@ describe('serveIndex(root)', function () {
.expect(200, done)
});

it('should not allow serving outside root', function (done) {
request(server)
.get('/../')
.set('Accept', 'text/html')
.expect(403, done)
});
if (major < NODE_BASE_VERSION) {
/**
* For Node versions lower than 10. To keep retrocompatibility
*
* Inside serveIndex function, it uses path.normalize()
* When it receives the path '/../' the normalize function resolves it to '/'
* Therefore, this test will pass
*
* @see https://nodejs.org/download/release/v18.16.0/docs/api/path.html#pathnormalizepath
*/
it('should not allow serving outside root', function (done) {
request(server)
.get('/../')
.set('Accept', 'text/html')
.expect(403, done)
});
}
});
});

Expand Down
Loading