<commit_before>import * as retry from 'bluebird-retry';
import * as chai from 'chai';
import { expect } from 'chai';
import * as fs from 'fs-extra-promise';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';

import {
    commands,
    TextEditor,
    Uri,
    window,
    workspace
} from 'vscode';

import {
    controller,
    duplicateFile
} from '../../src/extension/commands';

chai.use(sinonChai);

const rootDir = path.resolve(__dirname, '..', '..', '..');
const tmpDir = path.resolve(os.tmpdir(), 'vscode-fileutils-test--duplicate-file');

const fixtureFile1 = path.resolve(rootDir, 'test', 'fixtures', 'file-1.rb');
const fixtureFile2 = path.resolve(rootDir, 'test', 'fixtures', 'file-2.rb');

const editorFile1 = path.resolve(tmpDir, 'file-1.rb');
const editorFile2 = path.resolve(tmpDir, 'file-2.rb');

const targetFile = path.resolve(`${editorFile1}.tmp`);

describe('duplicateFile', () => {

    beforeEach(() => Promise.all([
        fs.removeAsync(tmpDir),
        fs.copyAsync(fixtureFile1, editorFile1),
        fs.copyAsync(fixtureFile2, editorFile2)
    ]));

    afterEach(() => fs.removeAsync(tmpDir));

    describe('as command', () => {

        describe('with open text document', () => {

            beforeEach(() => {

                const openDocument = () => {
                    const uri = Uri.file(editorFile1);
                    return workspace.openTextDocument(uri)
                        .then((textDocument) => window.showTextDocument(textDocument));
                };

                const stubShowInputBox = () => {
                    sinon.stub(window, 'showInputBox').returns(Promise.resolve(targetFile));
                    return Promise.resolve();
                };

                return Promise.all([
                    retry(() => openDocument(), { max_tries: 4, interval: 500 }),
                    stubShowInputBox()
                ]);
            });

            afterEach(() => {

                const closeAllEditors = () => {
                    return commands.executeCommand('workbench.action.closeAllEditors');
                };

                const restoreShowInputBox = () => {
                    const stub: any = window.showInputBox;
                    return Promise.resolve(stub.restore());
                };

                return Promise.all([
                    closeAllEditors(),
                    restoreShowInputBox()
                ]);
            });

            it('prompts for file destination', () => {

                return duplicateFile().then(() => {
                    const prompt = 'Duplicate As';
                    const value = editorFile1;
                    expect(window.showInputBox).to.have.been.calledWithExactly({ prompt, value });
                });

            });

            it('moves current file to destination', () => {

                return duplicateFile().then(() => {
                    const message = `${targetFile} does not exist`;
                    expect(fs.existsSync(targetFile), message).to.be.true;
                });
            });

            describe('target file in non existing nested directories', () => {

                const targetDir = path.resolve(tmpDir, 'level-1', 'level-2', 'level-3');

                beforeEach(() => {
                    const stub: any = window.showInputBox;
                    stub.returns(Promise.resolve(path.resolve(targetDir, 'file.rb')));
                });

                it('creates nested directories', () => {

                    return duplicateFile().then((textEditor: TextEditor) => {
                        const dirname = path.dirname(textEditor.document.fileName);
                        const directories: string[] = dirname.split(path.sep);

                        expect(directories.pop()).to.equal('level-3');
                        expect(directories.pop()).to.equal('level-2');
                        expect(directories.pop()).to.equal('level-1');
                    });
                });

            });

            it('opens target file as active editor', () => {

                return duplicateFile().then(() => {
                    const activeEditor: TextEditor = window.activeTextEditor;
                    expect(activeEditor.document.fileName).to.equal(targetFile);
                });
            });

            describe('when target destination exists', () => {

                beforeEach(() => {

                    const createTargetFile = () => {
                        return fs.copyAsync(editorFile2, targetFile);
                    };

                    const stubShowInformationMessage = () => {
                        sinon.stub(window, 'showInformationMessage').returns(Promise.resolve(true));
                        return Promise.resolve();
                    };

                    return Promise.all([
                        createTargetFile(),
                        stubShowInformationMessage()
                    ]);
                });

                afterEach(() => {
                    const stub: any = window.showInformationMessage;
                    return Promise.resolve(stub.restore());
                });

                it('asks to overwrite destination file', () => {

                    const message = `File '${targetFile}' already exists.`;
                    const action = 'Overwrite';
                    const options = { modal: true };

                    return duplicateFile().then(() => {
                        expect(window.showInformationMessage).to.have.been.calledWith(message, options, action);
                    });
                });

                describe('responding with yes', () => {

                    it('overwrites the existig file', () => {

                        return duplicateFile().then(() => {
                            const fileContent = fs.readFileSync(targetFile).toString();
                            expect(fileContent).to.equal('class FileOne; end');
                        });
                    });

                });

                describe('responding with no', () => {

                    beforeEach(() => {
                        const stub: any = window.showInformationMessage;<commit_msg>refactor(duplicate test): rename action names<commit_after>import * as retry from 'bluebird-retry';
import * as chai from 'chai';
import { expect } from 'chai';
import * as fs from 'fs-extra-promise';
import * as os from 'os';
import * as path from 'path';
import * as sinon from 'sinon';
import * as sinonChai from 'sinon-chai';

import {
    commands,
    TextEditor,
    Uri,
    window,
    workspace
} from 'vscode';

import {
    controller,
    duplicateFile
} from '../../src/extension/commands';

chai.use(sinonChai);

const rootDir = path.resolve(__dirname, '..', '..', '..');
const tmpDir = path.resolve(os.tmpdir(), 'vscode-fileutils-test--duplicate-file');

const fixtureFile1 = path.resolve(rootDir, 'test', 'fixtures', 'file-1.rb');
const fixtureFile2 = path.resolve(rootDir, 'test', 'fixtures', 'file-2.rb');

const editorFile1 = path.resolve(tmpDir, 'file-1.rb');
const editorFile2 = path.resolve(tmpDir, 'file-2.rb');

const targetFile = path.resolve(`${editorFile1}.tmp`);

describe('duplicateFile', () => {

    beforeEach(() => Promise.all([
        fs.removeAsync(tmpDir),
        fs.copyAsync(fixtureFile1, editorFile1),
        fs.copyAsync(fixtureFile2, editorFile2)
    ]));

    afterEach(() => fs.removeAsync(tmpDir));

    describe('as command', () => {

        describe('with open text document', () => {

            beforeEach(() => {

                const openDocument = () => {
                    const uri = Uri.file(editorFile1);
                    return workspace.openTextDocument(uri)
                        .then((textDocument) => window.showTextDocument(textDocument));
                };

                const stubShowInputBox = () => {
                    sinon.stub(window, 'showInputBox').returns(Promise.resolve(targetFile));
                    return Promise.resolve();
                };

                return Promise.all([
                    retry(() => openDocument(), { max_tries: 4, interval: 500 }),
                    stubShowInputBox()
                ]);
            });

            afterEach(() => {

                const closeAllEditors = () => {
                    return commands.executeCommand('workbench.action.closeAllEditors');
                };

                const restoreShowInputBox = () => {
                    const stub: any = window.showInputBox;
                    return Promise.resolve(stub.restore());
                };

                return Promise.all([
                    closeAllEditors(),
                    restoreShowInputBox()
                ]);
            });

            it('prompts for file destination', () => {

                return duplicateFile().then(() => {
                    const prompt = 'Duplicate As';
                    const value = editorFile1;
                    expect(window.showInputBox).to.have.been.calledWithExactly({ prompt, value });
                });

            });

            it('moves current file to destination', () => {

                return duplicateFile().then(() => {
                    const message = `${targetFile} does not exist`;
                    expect(fs.existsSync(targetFile), message).to.be.true;
                });
            });

            describe('target file in non existing nested directories', () => {

                const targetDir = path.resolve(tmpDir, 'level-1', 'level-2', 'level-3');

                beforeEach(() => {
                    const stub: any = window.showInputBox;
                    stub.returns(Promise.resolve(path.resolve(targetDir, 'file.rb')));
                });

                it('creates nested directories', () => {

                    return duplicateFile().then((textEditor: TextEditor) => {
                        const dirname = path.dirname(textEditor.document.fileName);
                        const directories: string[] = dirname.split(path.sep);

                        expect(directories.pop()).to.equal('level-3');
                        expect(directories.pop()).to.equal('level-2');
                        expect(directories.pop()).to.equal('level-1');
                    });
                });

            });

            it('opens target file as active editor', () => {

                return duplicateFile().then(() => {
                    const activeEditor: TextEditor = window.activeTextEditor;
                    expect(activeEditor.document.fileName).to.equal(targetFile);
                });
            });

            describe('when target destination exists', () => {

                beforeEach(() => {

                    const createTargetFile = () => {
                        return fs.copyAsync(editorFile2, targetFile);
                    };

                    const stubShowInformationMessage = () => {
                        sinon.stub(window, 'showInformationMessage').returns(Promise.resolve(true));
                        return Promise.resolve();
                    };

                    return Promise.all([
                        createTargetFile(),
                        stubShowInformationMessage()
                    ]);
                });

                afterEach(() => {
                    const stub: any = window.showInformationMessage;
                    return Promise.resolve(stub.restore());
                });

                it('asks to overwrite destination file', () => {

                    const message = `File '${targetFile}' already exists.`;
                    const action = 'Overwrite';
                    const options = { modal: true };

                    return duplicateFile().then(() => {
                        expect(window.showInformationMessage).to.have.been.calledWith(message, options, action);
                    });
                });

                describe(`responding with 'Overwrite'`, () => {

                    it('overwrites the existig file', () => {

                        return duplicateFile().then(() => {
                            const fileContent = fs.readFileSync(targetFile).toString();
                            expect(fileContent).to.equal('class FileOne; end');
                        });
                    });

                });

                describe(`responding with 'Cancel'`, () => {

                    beforeEach(() => {
                        const stub: any = window.showInformationMessage;
{'fraction_non_alphanumeric': 0.13257277734067663, 'fraction_numerical': 0.004091266719118804, 'mean_word_length': 1.4860160375513398, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 73, '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': '7470237', 'n_tokens_mistral': 3182, 'n_tokens_neox': 2929, 'n_words': 591}