Skip to content

Commit

Permalink
Do not format fixture on run (#924)
Browse files Browse the repository at this point in the history
* do not reformat JSON / JS fixtures, close #902

* do not reformat coffee fixtures

* remove html rewriting

* update JS rewriting tests

* remove .only
  • Loading branch information
bahmutov authored Nov 16, 2017
1 parent e7d82b6 commit a888f89
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 94 deletions.
34 changes: 0 additions & 34 deletions packages/server/lib/fixture.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ check = require("syntax-error")
coffee = require("../../../packages/coffee")
Promise = require("bluebird")
jsonlint = require("jsonlint")
beautify = require("js-beautify").html
pretty = require("js-object-pretty-print").pretty
formatter = require("jsonlint/lib/formatter").formatter
cwd = require("./cwd")

fs = Promise.promisifyAll(fs)
Expand Down Expand Up @@ -90,21 +87,6 @@ module.exports = {
parseJson: (p, fixture) ->
fs.readFileAsync(p, "utf8")
.bind(@)
.then (str) ->
## format the json
formatted = formatter.formatJson(str, " ")

## if we didnt change then return the str
if formatted is str
return str
else
## if last character is a new line
## then append this to the formatted str
if lastCharacterIsNewLine(str)
formatted += "\n"
## write the file back even if there were errors
## so we write back the formatted version of the str
fs.writeFileAsync(p, formatted).return(formatted)
.then(jsonlint.parse)
.catch (err) ->
throw new Error("'#{fixture}' is not valid JSON.\n#{err.message}")
Expand All @@ -121,9 +103,6 @@ module.exports = {
throw e

return obj
.then (obj) ->
str = pretty(obj, 2)
fs.writeFileAsync(p, str).return(obj)
.catch (err) ->
throw new Error("'#{fixture}' is not a valid JavaScript object.#{err.toString()}")

Expand All @@ -137,9 +116,6 @@ module.exports = {
.then (str) ->
str = coffee.compile(str, {bare: true})
eval(str)
.then (obj) ->
str = pretty(obj, 2)
fs.writeFileAsync(p, str).return(obj)
.catch (err) ->
throw new Error("'#{fixture} is not a valid CoffeeScript object.\n#{err.toString()}")
.finally ->
Expand All @@ -148,16 +124,6 @@ module.exports = {
parseHtml: (p, fixture) ->
fs.readFileAsync(p, "utf8")
.bind(@)
.then (str) ->
html = beautify str, {
indent_size: 2
extra_liners: []
}

if lastCharacterIsNewLine(str)
html += "\n"

fs.writeFileAsync(p, html).return(html)

parse: (p, fixture, encoding = "utf8") ->
fs.readFileAsync(p, encoding)
Expand Down
2 changes: 0 additions & 2 deletions packages/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@
"http-status-codes": "^1.0.6",
"human-interval": "^0.1.5",
"image-size": "^0.5.0",
"js-beautify": "^1.5.10",
"js-object-pretty-print": "^0.1.4",
"jsonlint": "^1.6.2",
"konfig": "^0.2.0",
"lazy-ass": "^1.6.0",
Expand Down
85 changes: 27 additions & 58 deletions packages/server/test/unit/fixture_spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ describe "lib/fixture", ->
"""
'bad_json.json' is not valid JSON.
Parse error on line 2:
{ "bad": "json""should": "not parse
----------------^
{ "bad": "json" "should": "not parse
------------------^
Expecting 'EOF', '}', ':', ',', ']', got 'STRING'
"""

Expand All @@ -69,28 +69,24 @@ describe "lib/fixture", ->
# on other platforms can match the error directly
expect(eol.auto(err.message)).to.eq eol.auto(e)

it "reformats json and writes back even on parse error", ->
it "does not reformat json on parse error", ->
fixture.get(@fixturesFolder, "bad_json.json")
.then ->
throw new Error("should have failed but did not")
.catch (err) =>
## ensure the bad_json file was rewritten even though there was a parse error
## ensure the bad_json file was kept as before
fs.readFileAsync(@fixturesFolder + "/bad_json.json", "utf8").then (str) ->
expect(str).to.eq """
{
"bad": "json""should": "not parse"
"bad": "json"
"should": "not parse"
}
"""

it "reformats json and writes this back", ->
it "does not reformat json or write fixture file", ->
fixture.get(@fixturesFolder, "no_format.json").then (obj) =>
fs.readFileAsync(@fixturesFolder + "/no_format.json", "utf8").then (json) ->
expect(json).to.eq """
{
"id": 1,
"name": "brian"
}
"""
expect(json).to.eq '{"id": 1, "name": "brian"}'

it "does not remove string whitespace", ->
fixture.get(@fixturesFolder, "words.json").then (obj) =>
Expand All @@ -114,7 +110,7 @@ describe "lib/fixture", ->
}
]

it "reformats empty objects", ->
it "does not reformat empty objects", ->
fn = =>
fixture.get(@fixturesFolder, "empty_objects")

Expand All @@ -123,10 +119,10 @@ describe "lib/fixture", ->
expect(str).to.eq """
{
"empty": {
"object": {\n \n },
"array": [\n \n ],
"object2": {\n \n },
"array2": [\n \n ]
"object": {},
"array": [],
"object2": {\n\n },
"array2": [\n\n ]
}
}
"""
Expand All @@ -141,16 +137,10 @@ describe "lib/fixture", ->
posts: []
}

it "rewrites file as a formated valid JS object", ->
it "does not rewrite file as a formated valid JS object", ->
fixture.get(@fixturesFolder, "no_format.js").then (obj) =>
fs.readFileAsync(@fixturesFolder + "/no_format.js", "utf8").then (str) ->
expect(str).to.eq """
{
foo: "bar",
baz: "quux"
}
"""
expect(str).to.eq '{foo: "bar", baz: "quux"}'

it "throws on a bad JS object", ->
e =
Expand All @@ -175,19 +165,14 @@ describe "lib/fixture", ->
users: []
}

it "rewrites file as formatted valid coffee object", ->
it "does not rewrite coffee files", ->
fixture.get(@fixturesFolder, "no_format.coffee").then =>
fs.readFileAsync(@fixturesFolder + "/no_format.coffee", "utf8").then (str) ->
expect(str).to.eq """
[
{
id: 1
},
{
id: 2
}
{id: 1}
{id: 2}
]
"""

it "throws on bad coffee object", ->
Expand All @@ -209,25 +194,25 @@ describe "lib/fixture", ->
<!doctype html>
<html>
<head>
<title>index.html</title>
<title>index.html</title>
</head>
<body>
index
index
</body>
</html>
"""

it "rewrites file as formatted html", ->
it "does not rewrite file as formatted html", ->
fixture.get(@fixturesFolder, "index.html").then =>
fs.readFileAsync(@fixturesFolder + "/index.html", "utf8").then (str) ->
expect(str).to.eq """
<!doctype html>
<html>
<head>
<title>index.html</title>
<title>index.html</title>
</head>
<body>
index
index
</body>
</html>
"""
Expand Down Expand Up @@ -336,35 +321,19 @@ describe "lib/fixture", ->
it "does not remove trailing new lines on .json", ->
fixture.get(@fixturesFolder, "trailing_new_line.json").then (str) =>
fs.readFileAsync(@fixturesFolder + "/trailing_new_line.json", "utf8").then (str2) ->
expect(str2).to.eq """
{
"foo": "bar"
}\n
"""
expect(str2).to.eq '{"foo": "bar"}\n'

it "does not remove trailing new lines on .js", ->
fixture.get(@fixturesFolder, "trailing_new_line.js").then (str) =>
fs.readFileAsync(@fixturesFolder + "/trailing_new_line.js", "utf8").then (str2) ->
expect(str2).to.eq """
{
foo: "bar"
}\n
"""
expect(str2).to.eq '{foo: "bar"}\n'

it "does not remove trailing new lines on .coffee", ->
fixture.get(@fixturesFolder, "trailing_new_line.coffee").then (str) =>
fs.readFileAsync(@fixturesFolder + "/trailing_new_line.coffee", "utf8").then (str2) ->
expect(str2).to.eq """
{
foo: "bar"
}\n
"""
expect(str2).to.eq '{ foo: "bar" }\n'

it "does not remove trailing new lines on .html", ->
fixture.get(@fixturesFolder, "trailing_new_line.html").then (str) =>
fs.readFileAsync(@fixturesFolder + "/trailing_new_line.html", "utf8").then (str2) ->
expect(str2).to.eq """
<html>
<body>foo</body>
</html>\n
"""
expect(str2).to.eq '<html><body>foo</body></html>\n'

0 comments on commit a888f89

Please sign in to comment.