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

Add resolve tests #153

Merged
merged 4 commits into from
Jan 19, 2016
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ postcssImport({

- Changed: support promise in `transform` option and `undefined` result will be skipped
([#147](https://github.com/postcss/postcss-import/pull/147))
- Added: detect css extension in package.json `main` field
([153](https://github.com/postcss/postcss-import/pull/153))

# 7.1.3 - 2015-11-05

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,17 @@ Checkout [tests](test) for more examples.
#### `root`

Type: `String`
Default: `process.cwd()`
Default: `process.cwd()` or _dirname of [the postcss `from`](https://github.com/postcss/postcss#node-source)_

Define the root where to resolve path (eg: place where `node_modules` are). Should not be used that much.
Define the root where to resolve path (eg: place where `node_modules` are). Should not be used that much.
_Note: nested `@import` will additionally benefit of the relative dirname of imported files._

#### `path`

Type: `String|Array`
Default: `process.cwd()` or _dirname of [the postcss `from`](https://github.com/postcss/postcss#node-source)_
Default: `[]`

A string or an array of paths in where to look for files.
_Note: nested `@import` will additionally benefit of the relative dirname of imported files._
A string or an array of paths in where to look for files.

#### `transform`

Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,12 @@ function AtImport(options) {
options.path = [ options.path ]
}

if (!Array.isArray(options.path)) {
options.path = []
}

options.path = options.path.map(function(p) {
return path.resolve(p)
return path.resolve(options.root, p)
})

return function(styles, result) {
Expand Down
62 changes: 20 additions & 42 deletions lib/resolve-id.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,10 @@
var fs = require("fs")
var path = require("path")
var resolve = require("resolve")

var moduleDirectories = [
"web_modules",
"node_modules",
]

function isFile(path) {
return new Promise(function(resolve, reject) {
fs.stat(path, function(err, stat) {
if (err) {
if (err.code === "ENOENT") {
return resolve(false)
}
return reject(err)
}
resolve(stat.isFile() || stat.isFIFO())
})
})
}

function resolveModule(id, opts) {
return new Promise(function(res, rej) {
resolve(id, opts, function(err, path) {
Expand All @@ -41,36 +25,30 @@ module.exports = function(id, base, options) {
paths: paths,
extensions: [ ".css" ],
packageFilter: function processPackage(pkg) {
pkg.main = pkg.style || "index.css"
if (pkg.style) {
pkg.main = pkg.style
}
else if (!pkg.main || !/\.css$/.test(pkg.main)) {
pkg.main = "index.css"
}
return pkg
},
}

// local detect
return resolveModule("./" + id, resolveOpts).catch(function() {
// modules and paths detect
return resolveModule("./" + id, resolveOpts)
.catch(function() {
return resolveModule(id, resolveOpts)
}).catch(function() {
// LAST HOPE
return Promise.all(paths.map(function(p) {
return isFile(path.resolve(p, id))
})).then(function(results) {
for (var i = 0; i < results.length; i += 1) {
if (results[i]) {
return path.resolve(paths[i], id)
}
}

if (paths.indexOf(base) === -1) {
paths.unshift(base)
}

throw new Error([
"Failed to find '" + id + "'",
"in [ ",
" " + paths.join(",\n "),
"]",
].join("\n "))
})
})
.catch(function() {
if (paths.indexOf(base) === -1) {
paths.unshift(base)
}

throw new Error([
"Failed to find '" + id + "'",
"in [ ",
" " + paths.join(",\n "),
"]",
].join("\n "))
})
}
1 change: 0 additions & 1 deletion test/fixtures/imports/local-module/main.css

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/imports/local-module/package.json

This file was deleted.

1 change: 1 addition & 0 deletions test/fixtures/imports/modules/empty/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
empty {}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/empty/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/main-js/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main-js {}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/main-js/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = {}
3 changes: 3 additions & 0 deletions test/fixtures/imports/modules/main-js/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "main.js"
}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/main-style/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style-mained {}
4 changes: 4 additions & 0 deletions test/fixtures/imports/modules/main-style/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"main": "main.css",
"style": "style.css"
}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/main-style/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main-styled {}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/main/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
main {}
3 changes: 3 additions & 0 deletions test/fixtures/imports/modules/main/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"main": "main.css"
}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/simple/index.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
simple {}
3 changes: 3 additions & 0 deletions test/fixtures/imports/modules/style/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"style": "style.css"
}
1 change: 1 addition & 0 deletions test/fixtures/imports/modules/style/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
style {}
5 changes: 0 additions & 5 deletions test/fixtures/imports/relative/baz.css

This file was deleted.

1 change: 0 additions & 1 deletion test/fixtures/imports/relative/import.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/imports/relative/qux.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/relative-to-source.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/relative-to-source.expected.css

This file was deleted.

3 changes: 0 additions & 3 deletions test/fixtures/relative.css

This file was deleted.

13 changes: 0 additions & 13 deletions test/fixtures/relative.expected.css

This file was deleted.

File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions test/fixtures/resolve-from.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "imports/foo.css";
@import "imports/foo-recursive.css";
3 changes: 3 additions & 0 deletions test/fixtures/resolve-from.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo{}
bar{}
foo.recursive{}
6 changes: 6 additions & 0 deletions test/fixtures/resolve-local-modules.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "fixtures/imports/modules/simple";
@import "fixtures/imports/modules/empty";
@import "fixtures/imports/modules/style";
@import "fixtures/imports/modules/main";
@import "fixtures/imports/modules/main-js";
@import "fixtures/imports/modules/main-style";
6 changes: 6 additions & 0 deletions test/fixtures/resolve-local-modules.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
simple {}
empty {}
style {}
main {}
main-js {}
main-styled {}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,3 @@
@import "web-use-dep";
@import "web-use-dep-too";
@import "web-use-dep" screen;

@import "local-module";

content{}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,3 @@
@media screen{
.web-dep{}
}
.local-module{}
content{}
2 changes: 2 additions & 0 deletions test/fixtures/resolve-path-cwd.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "foo.css";
@import "foo-recursive.css";
3 changes: 3 additions & 0 deletions test/fixtures/resolve-path-cwd.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo{}
bar{}
foo.recursive{}
6 changes: 6 additions & 0 deletions test/fixtures/resolve-path-modules.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
@import "simple";
@import "empty";
@import "style";
@import "main";
@import "main-js";
@import "main-style";
6 changes: 6 additions & 0 deletions test/fixtures/resolve-path-modules.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
simple {}
empty {}
style {}
main {}
main-js {}
main-styled {}
2 changes: 2 additions & 0 deletions test/fixtures/resolve-path-root.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "foo.css";
@import "foo-recursive.css";
3 changes: 3 additions & 0 deletions test/fixtures/resolve-path-root.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo{}
bar{}
foo.recursive{}
2 changes: 2 additions & 0 deletions test/fixtures/resolve-root.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "imports/foo.css";
@import "imports/foo-recursive.css";
3 changes: 3 additions & 0 deletions test/fixtures/resolve-root.expected.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo{}
bar{}
foo.recursive{}
29 changes: 0 additions & 29 deletions test/import.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,26 +27,6 @@ test("should ignore & adjust external import", t => {
return compareFixtures(t, "ignore")
})

test("should import stylsheets relatively", t => {
return compareFixtures(t, "relative")
})

test("should work without a specified path", t => {
return compareFixtures(t, "cwd")
})

test("should not need `path` option if `source` option has been passed", t => {
return compareFixtures(t, "relative-to-source", null, {
from: "fixtures/relative-to-source.css",
})
})

test("should be able to consume npm package or local modules", t => {
return compareFixtures(t, "modules", {
root: ".",
})
})

test("should not fail with only one absolute import", t => {
var base = "@import url(http://)"
return postcss()
Expand Down Expand Up @@ -100,15 +80,6 @@ test("should contain a correct sourcemap", t => {
})
})

test("import relative files using path option only", t => {
return postcss()
.use(atImport({ path: "fixtures/imports/relative" }))
.process(readFileSync("fixtures/imports/relative/import.css"))
.then(result => {
t.is(result.css, readFileSync("fixtures/imports/bar.css", "utf-8"))
})
})

test("inlined @import should keep PostCSS AST references clean", t => {
return postcss()
.use(atImport())
Expand Down
56 changes: 56 additions & 0 deletions test/resolve.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import test from "ava"
import compareFixtures from "./lib/compare-fixtures"

test("should resolve relative to cwd", t => {
return compareFixtures(t, "resolve-cwd", {
path: null,
})
})

test(`should resolve relative to 'root' option`, t => {
return compareFixtures(t, "resolve-root", {
root: "fixtures",
path: null,
})
})

test(`should resolve relative to postcss 'from' option`, t => {
return compareFixtures(t, "resolve-from", {
path: null,
}, {
from: "fixtures/file.css",
})
})

test(`should resolve relative to 'path' which resolved with cwd`, t => {
return compareFixtures(t, "resolve-path-cwd", {
path: "fixtures/imports",
})
})

test(`should resolve relative to 'path' which resolved with 'root'`, t => {
return compareFixtures(t, "resolve-path-root", {
root: "fixtures",
path: "imports",
})
})

test("should resolve local modules", t => {
return compareFixtures(t, "resolve-local-modules", {
path: null,
})
})

test("should resolve local modules", t => {
return compareFixtures(t, "resolve-path-modules", {
path: "fixtures/imports/modules",
})
})

test("should be able to consume npm package or local modules", t => {
return compareFixtures(t, "resolve-modules", {
path: null,
}, {
from: "fixtures/imports/foo.css",
})
})