# Kubos CLI
# Copyright (C) 2017 Kubos Corporation
#
# 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.import argparse

import argparse
import kubos
import os
import tempfile
import unittest

from kubos import init
from kubos.test import utils

class KubosInitTest(utils.KubosTestCase):

    def setUp(self):
        super(KubosInitTest, self).setUp()
        self.proj_name = 'test-project'
        self.args = argparse.Namespace()
        self.args.proj_name = [self.proj_name] # argparse returns proj_name as an array
        self.args.linux = False

    def test_creates_proj_dir(self):
        self.proj_dir = os.path.join(self.base_dir, self.proj_name)
        main_src_file = os.path.join(self.proj_dir, 'source', 'main.c')
        kubos.init.execCommand(self.args, None)
        self.assertTrue(os.path.isdir(self.proj_dir))
        self.assertTrue(os.path.isfile(main_src_file))


    def test_overwrite_existing(self):
        self.proj_name = 'test-project'
        self.proj_dir = os.path.join(self.base_dir, self.proj_name)
        with self.assertRaises(SystemExit):
            kubos.init.execCommand(self.args, None)
            #we cd into the project directory to change the module.json file in execCommand
            os.chdir(self.base_dir)
            kubos.init.execCommand(self.args, None)


    def tearDown(self):
        super(KubosInitTest, self).tearDown()

if __name__ == '__main__':
    unittest.main()

@@ -729,16 +729,134 @@
 rt utils
+%0Afrom kubos.utils import constants%0Afrom yotta.test.cli.test_target import Test_Module_JSON #A dummy module.json config
 %0A%0Aclass 
@@ -1161,135 +1161,876 @@
 lse%0A
-%0A    def test_creates_proj_dir(self):%0A        self.proj_dir = os.path.join(self.base_dir, self.proj_name)%0A        main_src_file
+        linux_module_json = os.path.join(constants.KUBOS_LINUX_EXAMPLE_DIR, 'module.json')%0A        rt_module_json = os.path.join(constants.KUBOS_RT_EXAMPLE_DIR, 'module.json')%0A%0A        #Set up the linux example module%0A        if not os.path.isdir(constants.KUBOS_LINUX_EXAMPLE_DIR):%0A            os.makedirs(constants.KUBOS_LINUX_EXAMPLE_DIR)%0A        if not os.path.isfile(linux_module_json):%0A            with open(linux_module_json, 'w') as mod_json:%0A                mod_json.write(Test_Module_JSON)%0A%0A        #Set up the rt example module%0A        if not os.path.isdir(constants.KUBOS_RT_EXAMPLE_DIR):%0A            os.makedirs(constants.KUBOS_RT_EXAMPLE_DIR)%0A        if not os.path.isfile(rt_module_json):%0A            with open(rt_module_json, 'w') as mod_json:%0A                mod_json.write(Test_Module_JSON)%0A%0A    def test_creates_proj_dir(self):%0A        self.proj_dir
  = o
@@ -2050,36 +2050,32 @@
 elf.
-proj
+base
 _dir, 
-'source', 'main.c'
+self.proj_name
 )%0A  
@@ -2178,63 +2178,8 @@
 r))%0A
-        self.assertTrue(os.path.isfile(main_src_file))%0A
 %0A%0A  

Fix init test by mocking out the example project locations if they do not exist