<commit_before>var test = require('tap').test,
    rework = require('rework'),
    reworkNpm = require('rework-npm'),
    fs = require('fs'),
    rimraf = require('rimraf'),
    assets = require('./');

// Hash of the fixtures/test.txt file
var HASH = 'a94a8fe5ccb19ba6';

test('copy asset files to directory', function(t) {
    rimraf.sync('build');

    var result = rework('.test { test: url(test.txt); }')
        .use(assets({
            src: 'fixtures',
            dest: 'build'
        }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(
        fs.existsSync('build/' + HASH + '.txt'),
        'copied file');

    t.end();
});

test('use different outputUrl', function(t) {
    rimraf.sync('build');

    var result = rework('.test { test: url(test.txt); }')
        .use(assets({
            src: 'fixtures',
            dest: 'build',
            prefix: 'test/'
        }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(test/' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(
        fs.existsSync('build/' + HASH + '.txt'),
        'copied file');

    t.end();
});

test('copy assets from nested directory', function(t) {
    rimraf.sync('build');

    var result = rework('.test { test: url(fixtures/test.txt); }')
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(
        fs.existsSync('build/' + HASH + '.txt'),
        'copied file');

    t.end();
});

test('replace multiple url calls', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: foo url(test.txt), url(test.txt) bar;',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({
            src: 'fixtures',
            dest: 'build'
        }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: foo url(' + HASH + '.txt), ' +
            'url(' + HASH + '.txt) bar;',
        '}'
    ].join('\n'));

    t.end();
});

test('use with importing modules', function(t) {
    rimraf.sync('build');

    var result = rework('@import "./fixtures/test.css";')
        .use(reworkNpm())
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(fs.existsSync('build/' + HASH + '.txt'), 'copied file');

    t.end();
});

test('do not copy absolute URLs', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(http://example.com/test.txt);',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, src);
    t.notOk(fs.existsSync('build'));
    t.end();
});

test('do not copy data URLs', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D);',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, src);
    t.notOk(fs.existsSync('build'));
    t.end();
});

test('allow onError to ignore errors', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(missing.txt);',
        '}'
    ].join('\n');

    var error = null;
    function onError(err) {
        error = err;
    }

    var result = rework(src)
        .use(assets({ dest: 'build', onError: onError }))
        .toString();<commit_msg>Add test for missing url path<commit_after>var test = require('tap').test,
    rework = require('rework'),
    reworkNpm = require('rework-npm'),
    fs = require('fs'),
    rimraf = require('rimraf'),
    assets = require('./');

// Hash of the fixtures/test.txt file
var HASH = 'a94a8fe5ccb19ba6';

test('copy asset files to directory', function(t) {
    rimraf.sync('build');

    var result = rework('.test { test: url(test.txt); }')
        .use(assets({
            src: 'fixtures',
            dest: 'build'
        }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(
        fs.existsSync('build/' + HASH + '.txt'),
        'copied file');

    t.end();
});

test('use different outputUrl', function(t) {
    rimraf.sync('build');

    var result = rework('.test { test: url(test.txt); }')
        .use(assets({
            src: 'fixtures',
            dest: 'build',
            prefix: 'test/'
        }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(test/' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(
        fs.existsSync('build/' + HASH + '.txt'),
        'copied file');

    t.end();
});

test('copy assets from nested directory', function(t) {
    rimraf.sync('build');

    var result = rework('.test { test: url(fixtures/test.txt); }')
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(
        fs.existsSync('build/' + HASH + '.txt'),
        'copied file');

    t.end();
});

test('replace multiple url calls', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: foo url(test.txt), url(test.txt) bar;',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({
            src: 'fixtures',
            dest: 'build'
        }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: foo url(' + HASH + '.txt), ' +
            'url(' + HASH + '.txt) bar;',
        '}'
    ].join('\n'));

    t.end();
});

test('use with importing modules', function(t) {
    rimraf.sync('build');

    var result = rework('@import "./fixtures/test.css";')
        .use(reworkNpm())
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, [
        '.test {',
        '  test: url(' + HASH + '.txt);',
        '}'
    ].join('\n'));

    t.ok(fs.existsSync('build/' + HASH + '.txt'), 'copied file');

    t.end();
});

test('do not copy absolute URLs', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(http://example.com/test.txt);',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, src);
    t.notOk(fs.existsSync('build'));
    t.end();
});

test('do not copy data URLs', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D);',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, src);
    t.notOk(fs.existsSync('build'));
    t.end();
});

test('do not copy files with no url', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(#test);',
        '}'
    ].join('\n');

    var result = rework(src)
        .use(assets({ dest: 'build' }))
        .toString();

    t.equal(result, src);
    t.notOk(fs.existsSync('build'));
    t.end();
})

test('allow onError to ignore errors', function(t) {
    rimraf.sync('build');

    var src = [
        '.test {',
        '  test: url(missing.txt);',
        '}'
    ].join('\n');

    var error = null;
    function onError(err) {
        error = err;
    }

    var result = rework(src)
        .use(assets({ dest: 'build', onError: onError }))
        .toString();
{'fraction_non_alphanumeric': 0.21628531253994632, 'fraction_numerical': 0.00306787677361626, 'mean_word_length': 1.8892171344165436, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4747408', 'n_tokens_mistral': 2751, 'n_tokens_neox': 2681, 'n_words': 544}