Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

'move' incorrect files name (angular_devkit/schematics/src/rules/move.ts) #13654

Closed
seneric opened this issue Feb 12, 2019 · 3 comments · Fixed by #13655
Closed

'move' incorrect files name (angular_devkit/schematics/src/rules/move.ts) #13654

seneric opened this issue Feb 12, 2019 · 3 comments · Fixed by #13655

Comments

@seneric
Copy link

seneric commented Feb 12, 2019

🐞 Bug report

Description

For example when you try use move('/myApp', '/')
at line 33 : tree.rename(path, toPath + '/' + path.substr(fromPath.length));
toPath + '/' + path.substr(fromPath.length) all files contain '///' before there name.

🔬 Minimal Reproduction

tree.create('a/b/file1', 'hello world');
move('a/b', '/') => file name is ///file1

A possible correction:
I replaced line 33:
tree.rename(path, toPath + '/' + path.substr(fromPath.length));

by

if (toPath === '/') {
  tree.rename(path, path.substr(fromPath.length));
} else {
  tree.rename(path, toPath + ((path.substr(fromPath.length))[0] === '/' ? '': '/') + path.substr(fromPath.length));
}

test file:

describe('move', () => {
    it('works on moving the whole structure', done => {
        const tree = new HostTree();
        tree.create('a/b/file1', 'hello world');
        tree.create('a/b/file2', 'hello world');
        tree.create('a/c/file3', 'hello world');

        callRule(move('sub'), observableOf(tree), context)
            .toPromise()
            .then(result => {
                expect(result.exists('sub/a/b/file1')).toBe(true);
                expect(result.exists('sub/a/b/file2')).toBe(true);
                expect(result.exists('sub/a/c/file3')).toBe(true);
            })
            .then(done, done.fail);
    });

    it('works on moving a subdirectory structure', done => {
        const tree = new HostTree();
        tree.create('a/b/file1', 'hello world');
        tree.create('a/b/file2', 'hello world');
        tree.create('a/c/file3', 'hello world');

        callRule(move('a/b', 'sub'), observableOf(tree), context)
            .toPromise()
            .then(result => {
                expect(result.exists('sub/file1')).toBe(true);
                expect(result.exists('sub/file2')).toBe(true);
                expect(result.exists('a/c/file3')).toBe(true);
            })
            .then(done, done.fail);
    });

    it('works on moving a directory into a subdirectory of itself', done => {
        const tree = new HostTree();
        tree.create('a/b/file1', 'hello world');
        tree.create('a/b/file2', 'hello world');
        tree.create('a/c/file3', 'hello world');

        callRule(move('a/b', 'a/b/c'), observableOf(tree), context)
            .toPromise()
            .then(result => {
                expect(result.exists('a/b/c/file1')).toBe(true);
                expect(result.exists('a/b/c/file2')).toBe(true);
                expect(result.exists('a/c/file3')).toBe(true);
            })
            .then(done, done.fail);
    });

    it('works on moving a directory into a parent of itself', done => {
        const tree = new HostTree();
        tree.create('a/b/file1', 'hello world');
        tree.create('a/b/file2', 'hello world');
        tree.create('a/c/file3', 'hello world');

        callRule(move('a/b', 'a'), observableOf(tree), context)
            .toPromise()
            .then(result => {
                expect(result.exists('file1')).toBe(false);
                expect(result.exists('file2')).toBe(false);
                expect(result.exists('a/file1')).toBe(true);
                expect(result.exists('a/file2')).toBe(true);
                expect(result.exists('a/c/file3')).toBe(true);
            })
            .then(done, done.fail);
    });

    it('becomes a noop with identical from and to', done => {
        const tree = new HostTree();
        tree.create('a/b/file1', 'hello world');
        tree.create('a/b/file2', 'hello world');
        tree.create('a/c/file3', 'hello world');

        callRule(move(''), observableOf(tree), context)
            .toPromise()
            .then(result => {
                expect(result.exists('a/b/file1')).toBe(true);
                expect(result.exists('a/b/file2')).toBe(true);
                expect(result.exists('a/c/file3')).toBe(true);
            })
            .then(done, done.fail);
    });

    it('works on moving a directory to root', done => {
        const tree = new HostTree();
        tree.create('a/b/file1', 'hello world');
        tree.create('a/b/file2', 'hello world');
        tree.create('a/c/file3', 'hello world');

        callRule(move('a/b', '/'), observableOf(tree), context)
            .toPromise()
            .then(result => {
                expect(result.exists('/file1')).toBe(true);
                expect(result.exists('/file2')).toBe(true);
                expect(result.exists('/file3')).toBe(false);
                expect(result.exists('a/c/file3')).toBe(true);
            })
            .then(done, done.fail);
    });
});

🌍 Your Environment


all angular-cli versions (even the last "master branch" version)
@alan-agius4
Copy link
Collaborator

alan-agius4 commented Feb 12, 2019

@seneric, I tried to replicate the issue, and it seems that during the rename the slashes are normalised and deduped properly. Infact trying to run the test you provided passes using the current master.

Maybe you can explain what is the problem that you are experiencing?

That said, in the line you mentioned we should probably use join. See: #13655

@seneric
Copy link
Author

seneric commented Feb 12, 2019

when you try use move('/myApp', '/') and debug the code when you spy what is contains in toPath + '/' + path.substr(fromPath.length) you have /// at the begining of the string (and it is not 'clean').
Perhaps using a join is cleaner ...

@alan-agius4 alan-agius4 removed the needs: more info Reporter must clarify the issue label Feb 12, 2019
alexeagle pushed a commit that referenced this issue Feb 13, 2019
…concatenation

This leads to cleaner paths while debugging

Fixes #13654
alexeagle pushed a commit that referenced this issue Feb 13, 2019
…concatenation

This leads to cleaner paths while debugging

Fixes #13654
@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Sep 9, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants