{"size": 2150, "ext": "js", "max_stars_repo_path": "test.js", "max_stars_repo_name": "jmerrifield/gulp-untar", "max_stars_repo_stars_event_min_datetime": "2015-01-13T20:05:42.000Z", "max_stars_repo_stars_event_max_datetime": "2015-01-13T20:05:42.000Z", "max_issues_repo_path": "test.js", "max_issues_repo_name": "jmerrifield/gulp-untar", "max_issues_repo_issues_event_min_datetime": "2015-01-13T19:44:20.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-31T23:04:29.000Z", "max_forks_repo_path": "test.js", "max_forks_repo_name": "jmerrifield/gulp-untar", "max_forks_repo_forks_event_min_datetime": "2015-02-26T18:19:53.000Z", "max_forks_repo_forks_event_max_datetime": "2020-03-20T04:26:31.000Z"}
{"max_stars_count": 1, "max_issues_count": 18, "max_forks_count": 12, "avg_line_length": 25.2941176471, "max_line_length": 96, "alphanum_fraction": 0.568372093}
var Vinyl = require('vinyl')
var assert = require('assert')
var fs = require('fs')
var _ = require('lodash')
var through = require('through2')
var untar = require('./index')

describe('gulp-untar', function () {
  function assertOutput(done) {
    var files = []

    return through.obj(function (file, enc, callback) {
      files.push(file)
      callback()
    }, function () {
      assert.equal(files.length, 2)

      var file1 = _.find(files, {path: 'fixtures/file1.txt', base: './fixtures', cwd: '.'})
      assert.ok(file1, 'No file found named "fixtures/file1.txt"')
      assert.ok(file1.isBuffer(), 'Expected buffer')
      assert.equal('File 1\n', file1.contents.toString())

      var file2 = _.find(files, {path: 'fixtures/dir1/file2.txt', base: './fixtures', cwd: '.'})
      assert.ok(file2, 'No file found named "fixtures/dir1/file2.txt"')
      assert.ok(file2.isBuffer(), 'Expected buffer')
      assert.equal('File 2\n', file2.contents.toString())

      done()
    })
  }

  context('in streaming mode', function () {
    it('should untar files', function (done) {
      var stream = untar()

      stream.pipe(assertOutput(done))

      stream.write(new Vinyl({
        path: './fixtures/test.tar',
        base: './fixtures',
        cwd: '.',
        contents: fs.createReadStream('./fixtures/test.tar')
      }))

      stream.end()
    })

  })

  context('in buffer mode', function () {
    it('should untar files', function (done) {
      var stream = untar()

      stream.pipe(assertOutput(done))

      stream.write(new Vinyl({
        path: './fixtures/test.tar',
        base: './fixtures',
        cwd: '.',
        contents: fs.readFileSync('./fixtures/test.tar')
      }))

      stream.end()
    })
  })

  context('null files', function () {
    it('should let them pass through', function (done) {
      var stream = untar()

      stream.on('data', function (file) {
        assert.equal(file.path, './fixtures/test.tar')
        done()
      })

      stream.write(new Vinyl({
        path: './fixtures/test.tar',
        base: './fixtures',
        cwd: '.',
        contents: null
      }))
    })
  })
})
