<commit_before>#
# Copyright 2012 Mortar Data Inc.
#
# Licensed 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.
#

require 'spec_helper'
require 'fakefs/spec_helpers'
require 'mortar/command/projects'
require 'launchy'
require "mortar/api"


module Mortar::Command
  describe Projects do
    
    before(:each) do
      stub_core      
      @git = Mortar::Git::Git.new
    end

    before("create") do 
      @tmpdir = Dir.mktmpdir
      Dir.chdir(@tmpdir)
    end
    
    project1 = {'name' => "Project1",
                'status' => Mortar::API::Projects::STATUS_ACTIVE,
                'git_url' => "git@github.com:mortarcode-dev/Project1"}
    project2 = {'name' => "Project2",
                'status' => Mortar::API::Projects::STATUS_ACTIVE,
                'git_url' => "git@github.com:mortarcode-dev/Project2"}
        
    context("index") do
      
      it "shows appropriate message when user has no projects" do
        mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => []}))
        
        stderr, stdout = execute("projects")
        stdout.should == <<-STDOUT
You have no projects.
STDOUT
      end
      
      it "shows appropriate message when user has multiple projects" do
        mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
        
        stderr, stdout = execute("projects")
        stdout.should == <<-STDOUT
=== projects
Project1
Project2

STDOUT
      end
    end
    
    context("delete") do
      it "shows error message when user doesn't include project name" do
        stderr, stdout = execute("projects:delete")
        stderr.should == <<-STDERR
 !    Usage: mortar projects:delete PROJECTNAME
 !    Must specify PROJECTNAME.
STDERR
      end
      
      it "deletes project" do
        project_name = "COMMANDO"

        mock(Mortar::Auth.api).delete_project(project_name).returns(Excon::Response.new(:body => {}, :status => 200 )) 
        stderr, stdout = execute("projects:delete COMMANDO")
        stdout.should == <<-STDOUT
Sending request to delete project: COMMANDO... done

Your project has been deleted.
STDOUT
      end
      
    end
    
    context("create") do

      it "generates and registers a project" do
        mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
        project_id = "1234abcd1234abcd1234"
        project_name = "some_new_project"
        project_git_url = "git@github.com:mortarcode-dev/#{project_name}"
        mock(Mortar::Auth.api).post_project("some_new_project") {Excon::Response.new(:body => {"project_id" => project_id})}
        mock(Mortar::Auth.api).get_project(project_id).returns(Excon::Response.new(:body => {"status" => Mortar::API::Projects::STATUS_ACTIVE,
                                                                                             "git_url" => project_git_url})).ordered
        mock(@git).remotes.with_any_args.returns({})
        mock(@git).remote_add("mortar", project_git_url)

        # if the git stuff doesn't work, the registration will fail, so we can pretend it does work here
        mock(@git).git("add .").returns(true)
        mock(@git).git("commit -m \"Mortar project scaffolding\"").returns(true)
        mock(@git).push_master

        stderr, stdout = execute("projects:create #{project_name}", nil, @git)
        Dir.pwd.end_with?("some_new_project").should be_true
        File.exists?("macros").should be_true
        File.exists?("fixtures").should be_true
        File.exists?("pigscripts").should be_true
        File.exists?("udfs").should be_true
        File.exists?("README.md").should be_true
        File.exists?("Gemfile").should be_false
        File.exists?("macros/.gitkeep").should be_true
        File.exists?("fixtures/.gitkeep").should be_true
        File.exists?("pigscripts/some_new_project.pig").should be_true
        File.exists?("udfs/python/some_new_project.py").should be_true

        File.read("pigscripts/some_new_project.pig").each_line { |line| line.match(/<%.*%>/).should be_nil }

        stdout.should == <<-STDOUT
\e[1;32m      create\e[0m  
\e[1;32m      create\e[0m  README.md
\e[1;32m      create\e[0m  .gitignore
\e[1;32m      create\e[0m  pigscripts
\e[1;32m      create\e[0m  pigscripts/some_new_project.pig
\e[1;32m      create\e[0m  controlscripts
\e[1;32m      create\e[0m  controlscripts/.gitkeep
\e[1;32m      create\e[0m  macros
\e[1;32m      create\e[0m  macros/.gitkeep
\e[1;32m      create\e[0m  fixtures
\e[1;32m      create\e[0m  fixtures/.gitkeep
\e[1;32m      create\e[0m  udfs
\e[1;32m      create\e[0m  udfs/python
\e[1;32m      create\e[0m  udfs/python/some_new_project.py
\e[1;32m      create\e[0m  udfs/jython
\e[1;32m      create\e[0m  udfs/jython/.gitkeep
\e[1;32m      create\e[0m  udfs/java
\e[1;32m      create\e[0m  udfs/java/.gitkeep
\e[1;32m      create\e[0m  vendor
\e[1;32m      create\e[0m  vendor/macros
\e[1;32m      create\e[0m  vendor/macros/.gitkeep
\e[1;32m      create\e[0m  vendor/udfs
\e[1;32m      create\e[0m  vendor/udfs/python
\e[1;32m      create\e[0m  vendor/udfs/python/.gitkeep
\e[1;32m      create\e[0m  vendor/udfs/jython
\e[1;32m      create\e[0m  vendor/udfs/jython/.gitkeep
\e[1;32m      create\e[0m  vendor/udfs/java
\e[1;32m      create\e[0m  vendor/udfs/java/.gitkeep
Sending request to register project: some_new_project... done\n\n\r\e[0KStatus: ACTIVE  \n\nYour project is ready for use.  Type 'mortar help' to see the commands you can perform on the project.\n
NOTE: You'll need to change to the new directory to use your project:
    cd some_new_project

STDOUT
      end
<commit_msg>fix unit test<commit_after>#
# Copyright 2012 Mortar Data Inc.
#
# Licensed 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.
#

require 'spec_helper'
require 'fakefs/spec_helpers'
require 'mortar/command/projects'
require 'launchy'
require "mortar/api"


module Mortar::Command
  describe Projects do
    
    before(:each) do
      stub_core      
      @git = Mortar::Git::Git.new
    end

    before("create") do 
      @tmpdir = Dir.mktmpdir
      Dir.chdir(@tmpdir)
    end
    
    project1 = {'name' => "Project1",
                'status' => Mortar::API::Projects::STATUS_ACTIVE,
                'git_url' => "<EMAIL>:mortarcode-dev/Project1"}
    project2 = {'name' => "Project2",
                'status' => Mortar::API::Projects::STATUS_ACTIVE,
                'git_url' => "git<EMAIL>:mortarcode-dev/Project2"}
        
    context("index") do
      
      it "shows appropriate message when user has no projects" do
        mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => []}))
        
        stderr, stdout = execute("projects")
        stdout.should == <<-STDOUT
You have no projects.
STDOUT
      end
      
      it "shows appropriate message when user has multiple projects" do
        mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
        
        stderr, stdout = execute("projects")
        stdout.should == <<-STDOUT
=== projects
Project1
Project2

STDOUT
      end
    end
    
    context("delete") do
      it "shows error message when user doesn't include project name" do
        stderr, stdout = execute("projects:delete")
        stderr.should == <<-STDERR
 !    Usage: mortar projects:delete PROJECTNAME
 !    Must specify PROJECTNAME.
STDERR
      end
      
      it "deletes project" do
        project_name = "COMMANDO"

        mock(Mortar::Auth.api).delete_project(project_name).returns(Excon::Response.new(:body => {}, :status => 200 )) 
        stderr, stdout = execute("projects:delete COMMANDO")
        stdout.should == <<-STDOUT
Sending request to delete project: COMMANDO... done

Your project has been deleted.
STDOUT
      end
      
    end
    
    context("create") do

      it "generates and registers a project" do
        mock(Mortar::Auth.api).get_projects().returns(Excon::Response.new(:body => {"projects" => [project1, project2]}))
        project_id = "1234abcd1234abcd1234"
        project_name = "some_new_project"
        project_git_url = "<EMAIL>:mortarcode-dev/#{project_name}"
        mock(Mortar::Auth.api).post_project("some_new_project") {Excon::Response.new(:body => {"project_id" => project_id})}
        mock(Mortar::Auth.api).get_project(project_id).returns(Excon::Response.new(:body => {"status" => Mortar::API::Projects::STATUS_ACTIVE,
                                                                                             "git_url" => project_git_url})).ordered
        mock(@git).remotes.with_any_args.returns({})
        mock(@git).remote_add("mortar", project_git_url)

        # if the git stuff doesn't work, the registration will fail, so we can pretend it does work here
        mock(@git).git("add .").returns(true)
        mock(@git).git("commit -m \"Mortar project scaffolding\"").returns(true)
        mock(@git).push_master

        stderr, stdout = execute("projects:create #{project_name}", nil, @git)
        Dir.pwd.end_with?("some_new_project").should be_true
        File.exists?("macros").should be_true
        File.exists?("fixtures").should be_true
        File.exists?("pigscripts").should be_true
        File.exists?("udfs").should be_true
        File.exists?("README.md").should be_true
        File.exists?("Gemfile").should be_false
        File.exists?("macros/.gitkeep").should be_true
        File.exists?("fixtures/.gitkeep").should be_true
        File.exists?("pigscripts/some_new_project.pig").should be_true
        File.exists?("udfs/python/some_new_project.py").should be_true

        File.read("pigscripts/some_new_project.pig").each_line { |line| line.match(/<%.*%>/).should be_nil }

        stdout.should == <<-STDOUT
\e[1;32m      create\e[0m  
\e[1;32m      create\e[0m  README.md
\e[1;32m      create\e[0m  .gitignore
\e[1;32m      create\e[0m  pigscripts
\e[1;32m      create\e[0m  pigscripts/some_new_project.pig
\e[1;32m      create\e[0m  controlscripts
\e[1;32m      create\e[0m  controlscripts/.gitkeep
\e[1;32m      create\e[0m  controlscripts/lib
\e[1;32m      create\e[0m  controlscripts/lib/__init__.py
\e[1;32m      create\e[0m  macros
\e[1;32m      create\e[0m  macros/.gitkeep
\e[1;32m      create\e[0m  fixtures
\e[1;32m      create\e[0m  fixtures/.gitkeep
\e[1;32m      create\e[0m  udfs
\e[1;32m      create\e[0m  udfs/python
\e[1;32m      create\e[0m  udfs/python/some_new_project.py
\e[1;32m      create\e[0m  udfs/jython
\e[1;32m      create\e[0m  udfs/jython/.gitkeep
\e[1;32m      create\e[0m  udfs/java
\e[1;32m      create\e[0m  udfs/java/.gitkeep
\e[1;32m      create\e[0m  vendor
\e[1;32m      create\e[0m  vendor/controlscripts
\e[1;32m      create\e[0m  vendor/controlscripts/.gitkeep
\e[1;32m      create\e[0m  vendor/controlscripts/lib
\e[1;32m      create\e[0m  vendor/controlscripts/lib/__init__.py
\e[1;32m      create\e[0m  vendor/pigscripts
\e[1;32m      create\e[0m  vendor/pigscripts/.gitkeep
\e[1;32m      create\e[0m  vendor/macros
\e[1;32m      create\e[0m  vendor/macros/.gitkeep
\e[1;32m      create\e[0m  vendor/udfs
\e[1;32m      create\e[0m  vendor/udfs/python
\e[1;32m      create\e[0m  vendor/udfs/python/.gitkeep
\e[1;32m      create\e[0m  vendor/udfs/jython
\e[1;32m      create\e[0m  vendor/udfs/jython/.gitkeep
\e[1;32m      create\e[0m  vendor/udfs/java
\e[1;32m      create\e[0m  vendor/udfs/java/.gitkeep
Sending request to register project: some_new_project... done\n\n\r\e[0KStatus: ACTIVE  \n\nYour project is ready for use.  Type 'mortar help' to see the commands you can perform on the project.\n
NOTE: You'll need to change to the new directory to use your project:
    cd some_new_project

STDOUT
      end

{'fraction_non_alphanumeric': 0.13189223057644112, 'fraction_numerical': 0.02568922305764411, 'mean_word_length': 2.8139187574671447, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 46, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2348033', 'n_tokens_mistral': 4482, 'n_tokens_neox': 4287, 'n_words': 926}