8000 Bracket file data in group named with filename. by rybesh · Pull Request #16 · noflo/noflo · GitHub
[go: up one dir, main page]

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
20 changes: 13 additions & 7 deletions src/components/ReadFile.coffee
Original file line number Diff line number Diff line change
@@ -1,24 +1,30 @@
# The ReadFile component receives a filename on the soure port, and sends
# contents the specified file to the out port. In case of errors the error
# message will be sent to the error port
# The ReadFile component receives a filename on the soure port, and
# sends the contents of the specified file to the out port. The filename
# is used to create a named group around the file contents data. In case
# of errors the error message will be sent to the error port.

fs = require "fs"
noflo = require "noflo"

class ReadFile extends noflo.Component
constructor: ->
@inPorts.source = new noflo.Port()
@outPorts.out = new noflo.Port()
@outPorts.error = new noflo.Port()
@inPorts =
source: new noflo.Port()
@outPorts =
out: new noflo.Port()
error: new noflo.Port()

@inPorts.source.on "data", (data) =>
@readFile data

readFile: (fileName) ->
fs.readFile fileName, "utf-8", (err, data) =>
if err
if err
@outPorts.error.send err
return @outPorts.error.disconnect()
@outPorts.out.beginGroup fileName
@outPorts.out.send data
@outPorts.out.endGroup()
@outPorts.out.disconnect()

exports.getComponent = ->
Expand Down
2 changes: 0 additions & 2 deletions src/lib/Component.coffee
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
events = require "events"

class Component extends events.EventEmitter
inPorts: {}
outPorts: {}
description: ""

getDescription: ->
Expand Down
40 changes: 40 additions & 0 deletions test/ReadFile.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
readfile = require "../src/components/ReadFile"
socket = require "../src/lib/InternalSocket"

setupComponent = ->
c = readfile.getComponent()
src = socket.createSocket()
out = socket.createSocket()
err = socket.createSocket()
c.inPorts.source.attach src
c.outPorts.out.attach out
c.outPorts.error.attach err
return [c, src, out, err]

exports["test error reading file"] = (test) ->
[c, src, out, err] = setupComponent()
err.once "data", (err) ->
test.equal err.errno, 34
test.equal err.code, 'ENOENT'
test.equal err.path, 'doesnotexist'
test.done()
src.send "doesnotexist"

exports["test reading file"] = (test) ->
[c, src, out, err] = setupComponent()
expect = "begingroup"
err.once "data", (err) ->
test.fail err.message
test.done()
out.once "begingroup", (group) ->
test.equal "begingroup", expect
test.equal group, "src/components/ReadFile.coffee"
expect = "data"
out.once "data", (data) ->
test.equal "data", expect
test.ok data.length > 0
expect = "endgroup"
out.once "endgroup", ->
test.equal "endgroup", expect
test.done()
src.send "src/components/ReadFile.coffee"
0