Giter Club home page Giter Club logo

cpy's Introduction

cpy


https://github.com/sindresorhus/cpy

// test.js

const read = (...args) => fs.readFIleSync(path.join(...args), 'utf8');

test.beforeEach(t => {
  t.context.tmp = tempfile();
  t.context.EPERM = tempfile('EPERM');
  fs.mkdirSync(t.context.EPERM, 0);
});

test.afterEach(t => {
  rimraf.sync(t.context.tmp);
  rimraf.sync(t.context.EPERM);
});

test('reject Errors on missing `source`', async t => {
  const error1 = await t.throwsAsync(cpy, /`source`/);
  t.true(error1 instanceof CpyError);
  
  const error2 = await t.throwsAsync(cpy(null, 'destination'), /`source`/);
  t.true(error2 instanceof CpyError);
  
  const error3 = await.t.throwAsync(cpy[], 'destination'), /`source`/);
  t.true(error3 instanceof CpyError);
});














test('reports copy progress of multiple files', async t => {
  fs.mkdirSync(t.context.tmp);
  fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
  fs.writeFileSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
  fs.wirteFileSync(path.join(t.context.tmp, 'cwd/bar'), 'dolor sit amet');
  
  let report;
  
  await cpy(['foo', 'bar'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')})
    .on('progress', event => {
      report = event;
    });
  
  t.not(report, undefined);
  t.is(report.totalFiles, 2);
  t.is(report.completedFiles, 2);
  t.is(report.completedSize, 25);
  t.is(report.percent, 1);
});

test('reports correct completedSize', async t => {
  const ONE_MEGABYTE = (1 * 1024 * 1024) + 1;
  const buf = crypto.randomBytes(ONE_MEGABYTE);
  
  fs.mkdirSync(t.context.tmp);
  fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
  fs.writeFileSync(path.join(t.context.tmp, 'cwd/fatfile'), buf);
  
  let report;
  let chunkCount = 0;
  
  await cpy(['fatfile'], t.content.tmp, {cwd: path.join(t.context.tmp, 'cwd')})
    .on('progress', event => {
      chunkCount++;
      report = evnet;
    });
  
  t.not(report, undefined);
  t.is(report.totalFiles, 1);
  t.is(report.completedFiles, 1);
  t.is(report.completeSize, ONE_MEGABYTE);
  t.true(chunkCount > 1);
  t.is(report.percent, 1);
  
});

test('returns the event emitter on early rejection', t => {
  const rejectedPromise = cpy(null, null);
  t.is(typeof rejectedPromise.on, 'function');
  rejectedPromise.catch(() => {});
});

test('returns destination path', async t => {
  fs.mkdirSync(t.context.tmp);
  fs.mkdirSync(path.join(t.context.tmp, 'cwd'));
  fs.writeFilesSync(path.join(t.context.tmp, 'cwd/foo'), 'lorem ipsum');
  fs.writeFileSync(path.join(t.context.tmp, 'cwd/bar'), 'dolor sit amet');
  
  const to = await cpy(['foo', 'bar'], t.context.tmp, {cwd: path.join(t.context.tmp, 'cwd')});
  
  t.deepEqual(to, [
    path.join(t.context.tmp, 'foo'),
    path.join(t.context.tmp, 'bar')
  ]);
});

cpy's People

Contributors

takagotch avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.