/**
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/
var cordova = require('../cordova'),
    path = require('path'),
    shell = require('shelljs'),
    fs = require('fs'),
    hooker = require('../src/hooker'),
    tempDir = path.join(__dirname, '..', 'temp'),
    fixturesDir = path.join(__dirname, 'fixtures'),
    testPlugin = path.join(fixturesDir, 'plugins', 'test'),
    cordova_project = path.join(fixturesDir, 'projects', 'cordova'),
    androidPlugin = path.join(fixturesDir, 'plugins', 'android');

var cwd = process.cwd();

describe('plugin command', function() {
    beforeEach(function() {
        // Make a temp directory
        shell.rm('-rf', tempDir);
        shell.mkdir('-p', tempDir);
    });

    it('should run inside a Cordova-based project', function() {
        this.after(function() {
            process.chdir(cwd);
        });

        cordova.create(tempDir);

        process.chdir(tempDir);

        expect(function() {
            cordova.plugin();
        }).not.toThrow();
    });
    it('should not run outside of a Cordova-based project', function() {
        this.after(function() {
            process.chdir(cwd);
        });

        process.chdir(tempDir);

        expect(function() {
            cordova.plugin();
        }).toThrow();
    });

    describe('`ls`', function() {
        beforeEach(function() {
            cordova.create(tempDir);
        });

        afterEach(function() {
            process.chdir(cwd);
        });

        it('should list out no plugins for a fresh project', function() {
            process.chdir(tempDir);

            expect(cordova.plugin('list')).toEqual('No plugins added. Use `cordova plugin add <plugin>`.');
        });
    });

    describe('`add`', function() {
        beforeEach(function() {
            cordova.create(tempDir);
            process.chdir(tempDir);
        });

        afterEach(function() {
            process.chdir(cwd);
        });
        describe('failure', function() {
            it('should throw if your app has no platforms added', function() {
                expect(function() {
                    cordova.plugin('add', testPlugin);
                }).toThrow('You need at least one platform added to your app. Use `cordova platform add <platform>`.');
            });
            it('should throw if plugin does not support any app platforms', function() {
                process.chdir(cordova_project);
                shell.mv('-f', path.join(cordova_project, 'platforms', 'android'), tempDir);
                shell.mv('-f', path.join(cordova_project, 'platforms', 'blackberry'), tempDir);
                this.after(function() {
                    process.chdir(cwd);
                    shell.mv('-f', path.join(tempDir, 'android'), path.join(cordova_project, 'platforms'));
                    shell.mv('-f', path.join(tempDir, 'blackberry'), path.join(cordova_project, 'platforms'));
                });
                expect(function() {
                    cordova.plugin('add', androidPlugin);
                }).toThrow('Plugin "android" does not support any of your application\'s platforms. Plugin platforms: android; your application\'s platforms: ios');
            });
            it('should throw if plugin is already added to project', function() {
                process.chdir(cordova_project);
                var cb = jasmine.createSpy();
                this.after(function() {
                    process.chdir(cordova_project);
                    cordova.plugin('rm', "test");
                    process.chdir(cwd);
                });
                runs(function() {
                    cordova.plugin('add', testPlugin, cb);
                });
                waitsFor(function() { return cb.wasCalled; }, 'frst add plugin');
                runs(function(){
                    expect(function() {
                        cordova.plugin('add', testPlugin);
                    }).toThrow('Plugin "test" already added to project.');
                });
            });
            it('should throw if plugin does not have a plugin.xml', function() {
                process.chdir(cordova_project);
                this.after(function() {
                    process.chdir(cwd);
                });
                expect(function() {
                    cordova.plugin('add', fixturesDir);
                }).toThrow('Plugin "fixtures" does not have a plugin.xml in the root. Plugin must support the Cordova Plugin Specification: https://github.com/alunny/cordova-plugin-spec');
            });
        });
    });
});


@@ -2751,1234 +2751,8 @@
 ) %7B%0A
-            it('should throw if your app has no platforms added', function() %7B%0A                expect(function() %7B%0A                    cordova.plugin('add', testPlugin);%0A                %7D).toThrow('You need at least one platform added to your app. Use %60cordova platform add %3Cplatform%3E%60.');%0A            %7D);%0A            it('should throw if plugin does not support any app platforms', function() %7B%0A                process.chdir(cordova_project);%0A                shell.mv('-f', path.join(cordova_project, 'platforms', 'android'), tempDir);%0A                shell.mv('-f', path.join(cordova_project, 'platforms', 'blackberry'), tempDir);%0A                this.after(function() %7B%0A                    process.chdir(cwd);%0A                    shell.mv('-f', path.join(tempDir, 'android'), path.join(cordova_project, 'platforms'));%0A                    shell.mv('-f', path.join(tempDir, 'blackberry'), path.join(cordova_project, 'platforms'));%0A                %7D);%0A                expect(function() %7B%0A                    cordova.plugin('add', androidPlugin);%0A                %7D).toThrow('Plugin %22android%22 does not support any of your application%5C's platforms. Plugin platforms: android; your application%5C's platforms: ios');%0A            %7D);%0A
     
@@ -2821,32 +2821,32 @@
 ', function() %7B%0A
+
                 
@@ -3487,49 +3487,8 @@
 row(
-'Plugin %22test%22 already added to project.'
 );%0A 
@@ -3839,24 +3839,24 @@
 xturesDir);%0A
+
             
@@ -3874,167 +3874,8 @@
 row(
-'Plugin %22fixtures%22 does not have a plugin.xml in the root. Plugin must support the Cordova Plugin Specification: https://github.com/alunny/cordova-plugin-spec'
 );%0A 

Fix 'plugin add' tests