var fs = require('fs'),
    path = require('path'),
    testResource = require('../../../test_resource'),
    forceUnlink = require('../../../../lib/file/force_unlink');

exports['Clean dead symlink.'] = function (test) {
    var filename = testResource.resolveMockFile('__mock_force_unlink_foo');
    forceUnlink._cleanDeadSymlink(filename, function (err) {
        test.ifError(err);
        forceUnlink._cleanDeadSymlink('/foo/bar/baz/_no_exists__', function (err) {
            test.ifError(err);
            test.done();
        });
    });
};

exports['Force unlink a file.'] = function (test) {
    testResource.prepareCleanWorkDirForTestFile(__filename, function (err, workDir) {
        var file1 = testResource.resolveWorkFile(workDir, 'foo'),
            file2 = testResource.resolveWorkFile(workDir, 'bar'),
            file3 = testResource.resolveMockFile(workDir, 'baz');
        fs.writeFile(file1, "bazbazbaz", function () {
            fs.chmod(file1, '444', function () {
                fs.symlink(file1, file2, function () {
                    forceUnlink(file1, function (err) {
                        test.ifError(err);
                        forceUnlink(file2, function (err) {
                            test.ifError(err);
                            forceUnlink(file3, function (err) {
                                test.ifError(err);
                                test.done();
                            });
                        });
                    });
                });
            });
        });
    });
};
exports['Handle errors.'] = function (test) {
    testResource.prepareCleanWorkDirForTestFile(__filename, function (err, workDir) {
        var workFile = path.resolve(workDir, 'work_file');
        fs.writeFile(workFile, 'foo', function () {
            var lstat = fs.lstat;
            fs.lstat = function (filename, callback) {
                callback(new Error('mock lstat error.'));
            };
            forceUnlink(workFile, function (err) {
                test.ok(err);
                fs.lstat = function (filename, callback) {
                    callback({
                        isSymbolicLink: function () {
                            return true;
                        }
                    });
                };
                var unlink = fs.unlink;
                fs.unlink = function (filename, callback) {
                    callback(new Error('mock unlink error'));
                };
                forceUnlink(workFile, function (err) {
                    test.ok(err);
                    fs.unlink = unlink;
                    fs.lstat = lstat;
                    var chmod = fs.chmod;
                    fs.chmod = function (filename, mode, callback) {
                        callback(new Error('mock chmod error.'));
                    };
                    forceUnlink(workFile, function (err) {
                        test.ok(err);
                        fs.chmod = chmod;
                        test.done();
                    });
                });
            });
        });
    });
};