@ -8,12 +8,17 @@
# remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#
import os
import json
import pathlib
import uuid
import pytest
from . import engine_template
import string
from o3de import engine_template
from unittest . mock import patch
TEST_TEMPLATED_CONTENT_WITH_LICENSE = """ \
/ / { BEGIN_LICENSE }
CPP_LICENSE_TEXT = """ // {BEGIN_LICENSE}
/ *
* All or portions of this file Copyright ( c ) Amazon . com , Inc . or its affiliates or
* its licensors .
@ -26,478 +31,198 @@ TEST_TEMPLATED_CONTENT_WITH_LICENSE = """\
*
* /
/ / { END_LICENSE }
#pragma once
#include <AzCore/EBus/EBus.h>
namespace $ { Name }
{
class $ { Name } Requests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / Put your public methods here
} ;
using $ { Name } RequestsBus = AZ : : EBus < $ { Name } Requests > ;
} / / namespace $ { Name }
"""
TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE = """ \
TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE = """
#pragma once
#include <AzCore/EBus/EBus.h>
#include <AzCore/Interface/Interface.h>
namespace $ { Name }
{
class $ { Name } Requests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
AZ_RTTI ( $ { Name } Requests , " { $ {Random_Uuid} } " ) ;
virtual ~ $ { Name } Requests ( ) = default ;
/ / Put your public methods here
} ;
using $ { Name } RequestsBus = AZ : : EBus < $ { Name } Requests > ;
} / / namespace $ { Name }
"""
TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITHOUT_LICENSE = """ \
#pragma once
#include <AzCore/EBus/EBus.h>
namespace TestTemplate
{
class TestTemplateRequests
class $ { Name } BusTraits
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
static constexpr AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static constexpr AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / Put your public methods here
} ;
using TestTemplateRequestsBus = AZ : : EBus < TestTemplateRequests > ;
} / / namespace TestTemplate
"""
TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITH_LICENSE = """ \
/ / { BEGIN_LICENSE }
/ *
* All or portions of this file Copyright ( c ) Amazon . com , Inc . or its affiliates or
* its licensors .
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution ( the " License " ) . All use of this software is governed by the License ,
* or , if provided , by the license below or the license accompanying this file . Do not
* remove or modify any license notices . This file is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
*
* /
/ / { END_LICENSE }
#pragma once
#include <AzCore/EBus/EBus.h>
namespace TestTemplate
{
class TestTemplateRequests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / Put your public methods here
} ;
using TestTemplateRequestsBus = AZ : : EBus < TestTemplateRequests > ;
} / / namespace TestTemplate
"""
TEST_CONCRETE_TESTPROJECT_TEMPLATE_CONTENT_WITHOUT_LICENSE = """ \
#pragma once
#include <AzCore/EBus/EBus.h>
namespace TestProject
{
class TestProjectRequests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / Put your public methods here
} ;
using TestProjectRequestsBus = AZ : : EBus < TestProjectRequests > ;
} / / namespace TestProject
"""
TEST_CONCRETE_TESTPROJECT_TEMPLATE_CONTENT_WITH_LICENSE = """ \
/ / { BEGIN_LICENSE }
/ *
* All or portions of this file Copyright ( c ) Amazon . com , Inc . or its affiliates or
* its licensors .
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution ( the " License " ) . All use of this software is governed by the License ,
* or , if provided , by the license below or the license accompanying this file . Do not
* remove or modify any license notices . This file is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
*
* /
/ / { END_LICENSE }
#pragma once
#include <AzCore/EBus/EBus.h>
namespace TestProject
{
class TestProjectRequests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / Put your public methods here
} ;
using TestProjectRequestsBus = AZ : : EBus < TestProjectRequests > ;
} / / namespace TestProject
using $ { Name } RequestBus = AZ : : EBus < $ { Name } Requests , $ { Name } BusTraits > ;
using $ { Name } Interface = AZ : : Interface < $ { Name } Requests > ;
} / / namespace $ { Name }
"""
TEST_CONCRETE_TESTGEM_TEMPLATE_CONTENT_WITHOUT_LICENSE = """ \
#pragma once
#include <AzCore/EBus/EBus.h>
namespace TestGem
{
class TestGemRequests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
TEST_TEMPLATED_CONTENT_WITH_LICENSE = CPP_LICENSE_TEXT + TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE
/ / Put your public methods here
} ;
TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITHOUT_LICENSE = string . Template (
TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE ) . safe_substitute ( { ' Name ' : " TestTemplate " } )
using TestGemRequestsBus = AZ : : EBus < TestGemRequests > ;
TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITH_LICENSE = string . Template (
TEST_TEMPLATED_CONTENT_WITH_LICENSE ) . safe_substitute ( { ' Name ' : " TestTemplate " } )
} / / namespace TestGem
TEST_CONCRETE_TESTPROJECT_TEMPLATE_CONTENT_WITHOUT_LICENSE = string . Template (
TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE ) . safe_substitute ( { ' Name ' : " TestProject " } )
"""
TEST_CONCRETE_TESTPROJECT_TEMPLATE_CONTENT_WITH_LICENSE = string . Template (
TEST_TEMPLATED_CONTENT_WITH_LICENSE ) . safe_substitute ( { ' Name ' : " TestProject " } )
TEST_CONCRETE_TESTGEM_TEMPLATE_CONTENT_WITH_LICENSE = """ \
/ / { BEGIN_LICENSE }
/ *
* All or portions of this file Copyright ( c ) Amazon . com , Inc . or its affiliates or
* its licensors .
*
* For complete copyright and license terms please see the LICENSE at the root of this
* distribution ( the " License " ) . All use of this software is governed by the License ,
* or , if provided , by the license below or the license accompanying this file . Do not
* remove or modify any license notices . This file is distributed on an " AS IS " BASIS ,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
*
* /
/ / { END_LICENSE }
#pragma once
TEST_CONCRETE_TESTGEM_TEMPLATE_CONTENT_WITHOUT_LICENSE = string . Template (
TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE ) . safe_substitute ( { ' Name ' : " TestGem " } )
#include <AzCore/EBus/EBus.h>
TEST_CONCRETE_TESTGEM_TEMPLATE_CONTENT_WITH_LICENSE = string . Template (
TEST_TEMPLATED_CONTENT_WITH_LICENSE ) . safe_substitute ( { ' Name ' : " TestGem " } )
namespace TestGem
TEST_TEMPLATE_JSON_CONTENTS = """ \
{
class TestGemRequests
: public AZ : : EBusTraits
{
public :
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / EBusTraits overrides
static const AZ : : EBusHandlerPolicy HandlerPolicy = AZ : : EBusHandlerPolicy : : Single ;
static const AZ : : EBusAddressPolicy AddressPolicy = AZ : : EBusAddressPolicy : : Single ;
/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /
/ / Put your public methods here
} ;
using TestGemRequestsBus = AZ : : EBus < TestGemRequests > ;
} / / namespace TestGem
"""
TEST_DEFAULTTEMPLATE_JSON_CONTENTS = """ \
{
" inputPath " : " Templates/Default/Template " ,
" template_name " : " Templates " ,
" origin " : " The primary repo for Templates goes here: i.e. http://www.mydomain.com " ,
" license " : " What license Templates uses goes here: i.e. https://opensource.org/licenses/MIT " ,
" display_name " : " Templates " ,
" summary " : " A short description of Templates. " ,
" canonical_tags " : [ ] ,
" user_tags " : [
" Templates "
] ,
" icon_path " : " preview.png " ,
" copyFiles " : [
{
" inF ile" : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" o utFile " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" file " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" origin " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" isTemplated " : true ,
" isOptional " : false
}
] ,
" createDirectories " : [
{
" outDir " : " Code "
} ,
{
" outDir " : " Code/Include "
} ,
{
" outDir " : " Code/Include/Platform "
} ,
{
" outDir " : " Code/Include/$ {Name} "
}
]
} \
"""
TEST_DEFAULTTEMPLATE_RESTRICTED_JSON_CONTENTS = """ \
{
" inputPath " : " restricted/Salem/Templates/Default/Template " ,
" copyFiles " : [
{
" inFile " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" outFile " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" file " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" origin " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" isTemplated " : true ,
" isOptional " : false
}
] ,
" createDirectories " : [
{
" outDir " : " Code/Include/Platform/Salem "
}
]
} \
"""
TEST_DEFAULTPROJECT_TEMPLATE_JSON_CONTENTS = """ \
{
" inputPath " : " Templates/DefaultProject/Template " ,
" copyFiles " : [
{
" inFile " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" outFile " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" isTemplated " : true ,
" isOptional " : false
}
] ,
" createDirectories " : [
" dir " : " Code " ,
" origin " : " Code "
} ,
{
" outDir " : " Code "
" dir " : " Code/Include " ,
" origin " : " Code/Include "
} ,
{
" outDir " : " Code/Include "
" dir " : " Code/Include/$ {Name} " ,
" origin " : " Code/Include/$ {Name} "
} ,
{
" outDir " : " Code/Include/Platform "
" dir " : " Code/Include/Platform " ,
" origin " : " Code/Include/Platform "
} ,
{
" outDir " : " Code/Include/$ {Name} "
" dir " : " Code/Include/Platform/Salem " ,
" origin " : " Code/Include/Platform/Salem "
}
]
} \
}
"""
TEST_DEFAULTPROJECT_TEMPLATE_RESTRICTED_JSON_CONTENTS = """ \
{
" inputPath " : " restricted/Salem/Templates/DefaultProject/Template " ,
" copyFiles " : [
{
" inFile " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" outFile " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" isTemplated " : true ,
" isOptional " : false
}
] ,
" createDirectories " : [
{
" outDir " : " Code/Include/Platform/Salem "
}
]
} \
"""
TEST_DEFAULTGEM_TEMPLATE_JSON_CONTENTS = """ \
{
" inputPath " : " Templates/DefaultGem/Template " ,
" copyFiles " : [
{
" inFile " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" outFile " : " Code/Include/$ {Name} /$ {Name} Bus.h " ,
" isTemplated " : true ,
" isOptional " : false
}
] ,
" createDirectories " : [
{
" outDir " : " Code "
} ,
{
" outDir " : " Code/Include "
} ,
{
" outDir " : " Code/Include/Platform "
} ,
{
" outDir " : " Code/Include/$ {Name} "
}
]
} \
"""
TEST_CONCRETE_TEMPLATE_JSON_CONTENTS = string . Template (
TEST_TEMPLATE_JSON_CONTENTS ) . safe_substitute ( { ' Name ' : ' TestTemplate ' } )
TEST_CONCRETE_PROJECT_TEMPLATE_JSON_CONTENTS = string . Template (
TEST_TEMPLATE_JSON_CONTENTS ) . safe_substitute ( { ' Name ' : ' TestProject ' } )
TEST_DEFAULTGEM_TEMPLATE_RESTRICTED_JSON_CONTENTS = """ \
{
" inputPath " : " restricted/Salem/Templates/DefaultGem/Template " ,
" copyFiles " : [
{
" inFile " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" outFile " : " Code/Include/Platform/Salem/$ {Name} Bus.h " ,
" isTemplated " : true ,
" isOptional " : false
}
] ,
" createDirectories " : [
{
" outDir " : " Code/Include/Platform/Salem "
}
]
} \
"""
TEST_CONCRETE_GEM_TEMPLATE_JSON_CONTENTS = string . Template (
TEST_TEMPLATE_JSON_CONTENTS ) . safe_substitute ( { ' Name ' : ' TestGem ' } )
@pytest.mark.parametrize (
" concrete_contents, "
" templated_contents_with_license, templated_contents_without_license, "
" keep_license_text, expect_failure,"
" template_json_contents , restricted_template_json_contents " , [
" keep_license_text, force, expect_failure, "
" template_json_contents " , [
pytest . param ( TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITH_LICENSE ,
TEST_TEMPLATED_CONTENT_WITH_LICENSE , TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE ,
True , False,
TEST_ DEFAULT TEMPLATE_JSON_CONTENTS, TEST_DEFAULTTEMPLATE_RESTRICTED _JSON_CONTENTS) ,
True , True , False ,
TEST_TEMPLATE_JSON_CONTENTS ) ,
pytest . param ( TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITH_LICENSE ,
TEST_TEMPLATED_CONTENT_WITH_LICENSE , TEST_TEMPLATED_CONTENT_WITHOUT_LICENSE ,
False , False,
TEST_ DEFAULT TEMPLATE_JSON_CONTENTS, TEST_DEFAULTTEMPLATE_RESTRICTED _JSON_CONTENTS)
False , True , False ,
TEST_TEMPLATE_JSON_CONTENTS )
]
)
def test_create_template ( tmpdir ,
concrete_contents ,
templated_contents_with_license , templated_contents_without_license ,
keep_license_text , expect_failure ,
template_json_contents , restricted_template_json_contents ) :
dev_root = str ( tmpdir . join ( ' dev ' ) . realpath ( ) ) . replace ( ' \\ ' , ' / ' )
os . makedirs ( dev_root , exist_ok = True )
dev_gem_code_include_testgem = f ' { dev_root } /TestTemplate/Code/Include/TestTemplate '
os . makedirs ( dev_gem_code_include_testgem , exist_ok = True )
gem_bus_file = f ' { dev_gem_code_include_testgem } /TestTemplateBus.h '
if os . path . isfile ( gem_bus_file ) :
os . unlink ( gem_bus_file )
with open ( gem_bus_file , ' w ' ) as s :
s . write ( concrete_contents )
keep_license_text , force , expect_failure ,
template_json_contents ) :
engine_root = ( pathlib . Path ( tmpdir ) / ' engine-root ' ) . resolve ( )
engine_root . mkdir ( parents = True , exist_ok = True )
dev_gem_code_include_platform_salem = f ' { dev_root } /TestTemplate/Code/Include/Platform/Salem '
os . makedirs ( dev_gem_code_include_platform_salem , exist_ok = True )
template_source_path = engine_root / ' TestTemplates '
restricted_gem_bus_file = f ' { dev_gem_code_include_platform_salem } /TestTemplateBus.h '
if os . path . isfile ( restricted_gem_bus_file ) :
os . unlink ( restricted_gem_bus_file )
with open ( restricted_gem_bus_file , ' w ' ) as s :
engine_gem_code_include_testgem = template_source_path / ' Code/Include/TestTemplate '
engine_gem_code_include_testgem . mkdir ( parents = True , exist_ok = True )
gem_bus_file = engine_gem_code_include_testgem / ' TestTemplateBus.h '
with gem_bus_file . open ( ' w ' ) as s :
s . write ( concrete_contents )
template_folder = f ' { dev_root } /Templates '
os . makedirs ( template_folder , exist_ok = True )
engine_gem_code_include_platform_salem = template_source_path / ' Code/Include/Platform/Salem '
engine_gem_code_include_platform_salem . mkdir ( parents = True , exist_ok = True )
restricted_gem_bus_file = engine_gem_code_include_platform_salem / ' TestTemplateBus.h '
with restricted_gem_bus_file . open ( ' w ' ) as s :
s . write ( concrete_contents )
restricted_folder = f ' { dev_root } /restricted '
os . makedirs ( restricted_folder , exist_ok = True )
template_folder = engine_root / ' Templates '
template_folder. mkdir ( parents = True , exist_ok = True )
result = engine_template . create_template ( dev_root , ' TestTemplate ' , ' Default ' , keep_license_text = keep_license_text )
result = engine_template . create_template ( template_source_path , template_folder , source_name = ' TestTemplate ' ,
keep_license_text = keep_license_text , force = force )
if expect_failure :
assert result != 0
else :
assert result == 0
new_template_folder = f ' { template_folder } /Default '
assert os. path . isdir ( new_template_folder)
new_template_json = f ' { new_template_folder } / template.json'
assert os. path . isfile ( new_template_json)
with open ( new_template_json , ' r ' ) as s :
new_template_folder = template_folder
assert new_template_folder. is_dir ( )
new_template_json = new_template_folder / ' template.json'
assert new_template_json. is_file ( )
with new_template_json . open ( ' r ' ) as s :
s_data = s . read ( )
assert s_data == template_json_contents
assert json. loads ( s_data) == json. loads ( template_json_contents)
new_default_name_bus_file = f ' { new_template_folder } /Template/Code/Include/ ' + ' $ {Name} /$ {Name} Bus.h '
assert os . path . isfile ( new_default_name_bus_file )
with open ( new_default_name_bus_file , ' r ' ) as s :
template_content_folder = new_template_folder / ' Template '
new_default_name_bus_file = template_content_folder / ' Code/Include/$ {Name} /$ {Name} Bus.h '
assert new_default_name_bus_file . is_file ( )
with new_default_name_bus_file . open ( ' r ' ) as s :
s_data = s . read ( )
if keep_license_text :
assert s_data == templated_contents_with_license
else :
assert s_data == templated_contents_without_license
restricted_template_folder = f ' { dev_root } /restricted/Salem/Templates '
new_restricted_template_folder = f ' { restricted_template_folder } /Default '
assert os . path . isdir ( new_restricted_template_folder )
new_restricted_template_json = f ' { new_restricted_template_folder } /template.json '
assert os . path . isfile ( new_restricted_template_json )
with open ( new_restricted_template_json , ' r ' ) as s :
s_data = s . read ( )
assert s_data == restricted_template_json_contents
platform_template_folder = engine_root / ' Salem/Templates '
new_restricted_default_name_bus_file = f ' { restricted_template_folder } ' \
f ' /Default/Template/Code/Include/Platform/Salem/ ' + ' $ {Name} Bus.h '
assert os . path . isfile ( new_restricted_default_name_bus_file )
with open ( new_restricted_default_name_bus_file , ' r ' ) as s :
new_platform_default_name_bus_file = template_content_folder / ' Code/Include/Platform/Salem/$ {Name} Bus.h '
assert new_platform_default_name_bus_file . is_file ( )
with new_platform_default_name_bus_file . open ( ' r ' ) as s :
s_data = s . read ( )
if keep_license_text :
assert s_data == templated_contents_with_license
@ -505,244 +230,164 @@ def test_create_template(tmpdir,
assert s_data == templated_contents_without_license
@pytest.mark.parametrize (
" concrete_contents, templated_contents, "
" keep_license_text, expect_failure, "
" template_json_contents, restricted_template_json_contents " , [
pytest . param ( TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITH_LICENSE , TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
True , False ,
TEST_DEFAULTTEMPLATE_JSON_CONTENTS , TEST_DEFAULTTEMPLATE_RESTRICTED_JSON_CONTENTS ) ,
pytest . param ( TEST_CONCRETE_TESTTEMPLATE_CONTENT_WITHOUT_LICENSE , TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
False , False ,
TEST_DEFAULTTEMPLATE_JSON_CONTENTS , TEST_DEFAULTTEMPLATE_RESTRICTED_JSON_CONTENTS )
]
)
def test_create_from_template ( tmpdir ,
class TestCreateTemplate :
def instantiate_template_wrapper ( self , tmpdir , create_from_template_func , instantiated_name ,
concrete_contents , templated_contents ,
keep_license_text , expect_failure ,
template_json_contents , restricted_template_json_contents ) :
dev_root = str ( tmpdir . join ( ' dev ' ) . realpath ( ) ) . replace ( ' \\ ' , ' / ' )
os . makedirs ( dev_root , exist_ok = True )
template_default_folder = f ' { dev_root } /Templates/Default '
os . makedirs ( template_default_folder , exist_ok = True )
template_json = f ' { template_default_folder } /template.json '
if os . path . isfile ( template_json ) :
os . unlink ( template_json )
with open ( template_json , ' w ' ) as s :
s . write ( template_json_contents )
keep_license_text , force , expect_failure ,
template_json_contents , template_file_creation_map = { } ,
* * create_from_template_kwargs ) :
# Use a SHA-1 Hash of the destination_name for every Random_Uuid for determinism in the test
concrete_contents = string . Template ( concrete_contents ) . safe_substitute (
{ ' Random_Uuid ' : uuid . uuid5 ( uuid . NAMESPACE_DNS , instantiated_name ) } )
default_name_bus_dir = f ' { template_default_folder } /Template/Code/Include/ ' + ' $ {Name} '
os . makedirs ( default_name_bus_dir , exist_ok = True )
engine_root = ( pathlib . Path ( tmpdir ) / ' engine-root ' ) . resolve ( )
engine_root . mkdir ( parents = True , exist_ok = True )
default_name_bus_file = f ' { default_name_bus_dir } / ' + ' $ {Name} Bus.h '
if os . path . isfile ( default_name_bus_file ) :
os . unlink ( default_name_bus_file )
with open ( default_name_bus_file , ' w ' ) as s :
s . write ( templated_contents )
template_default_folder = engine_root / ' Templates/Default '
template_default_folder . mkdir ( parents = True , exist_ok = True )
template_json = template_default_folder / ' template.json '
with template_json . open ( ' w ' ) as s :
s . write ( template_json_contents )
restricted_template_default_folder = f ' { dev_root } /restricted/Salem/Templates/Default '
os . makedirs ( restricted_template_default_folder , exist_ok = True )
for file_template_filename , file_template_content in template_file_creation_map . items ( ) :
file_template_path = template_default_folder / ' Template ' / file_template_filename
file_template_path . parent . mkdir ( parents = True , exist_ok = True )
with file_template_path . open ( ' w ' ) as file_template_handle :
file_template_handle . write ( file_template_content )
restricted_template_json = f ' { restricted_template_default_folder } /template.json '
if os . path . isfile ( restricted_template_json ) :
os . unlink ( restricted_template_json )
with open ( restricted_template_json , ' w ' ) as s :
s . write ( restricted_template_json_contents )
default_name_bus_dir = template_default_folder / ' Template/Code/Include/$ {Name} '
default_name_bus_dir . mkdir ( parents = True , exist_ok = True )
restricted_default_name_bus_dir = f ' { restricted_template_default_folder } /Template/Code/Include/Platform/Salem '
os . makedirs ( restricted_default_name_bus_dir , exist_ok = True )
default_name_bus_file = default_name_bus_dir / ' $ {Name} Bus.h '
with default_name_bus_file . open ( ' w ' ) as s :
s . write ( templated_contents )
template_content_folder = template_default_folder / ' Template '
platform_default_name_bus_dir = template_content_folder / ' Code/Include/Platform/Salem '
platform_default_name_bus_dir . mkdir ( parents = True , exist_ok = True )
restricted_default_name_bus_file = f ' { restricted_default_name_bus_dir } / ' + ' $ {Name} Bus.h '
if os . path . isfile ( restricted_default_name_bus_file ) :
os . unlink ( restricted_default_name_bus_file )
with open ( restricted_default_name_bus_file , ' w ' ) as s :
platform_default_name_bus_file = platform_default_name_bus_dir / ' $ {Name} Bus.h '
with platform_default_name_bus_file . open ( ' w ' ) as s :
s . write ( templated_contents )
result = engine_template . create_from_template ( dev_root , ' TestTemplate ' , ' Default ' ,
keep_license_text = keep_license_text )
template_dest_path = engine_root / instantiated_name
with patch ( ' uuid.uuid4 ' , return_value = uuid . uuid5 ( uuid . NAMESPACE_DNS , instantiated_name ) ) as uuid4_mock :
result = create_from_template_func ( template_dest_path , template_path = template_default_folder , force = True ,
keep_license_text = keep_license_text , * * create_from_template_kwargs )
if expect_failure :
assert result != 0
else :
assert result == 0
test_folder = f ' { dev_root } /TestTemplate '
assert os . path . isdir ( test_folder )
test_folder = template_dest_path
assert test_folder . is_dir ( )
test_bus_file = f ' { test_folder } /Code/Include/TestTemplate/TestTemplate Bus.h'
assert os . path . isfile ( test_bus_file )
with open ( test_bus_file , ' r ' ) as s :
test_bus_file = test_folder / f ' Code/Include/ { instantiated_name } / { instantiated_name } Bus.h'
assert test_bus_file . is_file ( )
with test_bus_file . open ( ' r ' ) as s :
s_data = s . read ( )
assert s_data == concrete_contents
restricted_test_bus_folder = f ' { dev_root } /restricted/Salem/TestTemplate/ Code/Include/Platform/Salem'
assert os . path . isdir ( restricted_test_bus_folder )
platform_test_bus_folder = test_folder / ' Code/Include/Platform/Salem'
assert platform_test_bus_folder . is_dir ( )
restricted_default_name_bus_file = f ' { restricted_test_bus_folder } /TestTemplate Bus.h'
assert os . path . isfile ( restricted_default_name_bus_file )
with open ( restricted_default_name_bus_file , ' r ' ) as s :
platform_default_name_bus_file = platform_test_bus_folder / f ' { instantiated_name } Bus.h'
assert platform_default_name_bus_file . is_file ( )
with platform_default_name_bus_file . open ( ' r ' ) as s :
s_data = s . read ( )
assert s_data == concrete_contents
@pytest.mark.parametrize (
# Use a SHA-1 Hash of the destination_name for every Random_Uuid for determinism in the test
@pytest.mark.parametrize (
" concrete_contents, templated_contents, "
" keep_license_text , expect_failure,"
" template_json_contents, restricted_ template_json_contents" , [
pytest . param ( TEST_CONCRETE_TEST PROJECT_ TEMPLATE_CONTENT_WITH_LICENSE, TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
True , False ,
TEST_DEFAULTPROJECT_TEMPLATE_JSON_CONTENTS , TEST_DEFAULTPROJECT_TEMPLATE_RESTRICTED _JSON_CONTENTS) ,
pytest . param ( TEST_CONCRETE_TEST PROJECT_ TEMPLATE_CONTENT_WITHOUT_LICENSE, TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
Fals e, False ,
TEST_DEFAULTPROJECT_TEMPLATE_JSON_CONTENTS , TEST_DEFAULTPROJECT_TEMPLATE_RESTRICTED _JSON_CONTENTS)
" keep_license_text , force , expect_failure,"
" template_json_contents" , [
pytest . param ( TEST_CONCRETE_TEST TEMPLATE_CONTENT_WITH_LICENSE, TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
True , True , False ,
TEST_TEMPLATE _JSON_CONTENTS) ,
pytest . param ( TEST_CONCRETE_TEST TEMPLATE_CONTENT_WITHOUT_LICENSE, TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
Fals e, Tru e, False ,
TEST_TEMPLATE _JSON_CONTENTS)
]
)
def test_create_project ( tmpdir ,
concrete_contents , templated_contents ,
keep_license_text , expect_failure ,
template_json_contents , restricted_template_json_contents ) :
dev_root = str ( tmpdir . join ( ' dev ' ) . realpath ( ) ) . replace ( ' \\ ' , ' / ' )
os . makedirs ( dev_root , exist_ok = True )
template_default_folder = f ' { dev_root } /Templates/DefaultProject '
os . makedirs ( template_default_folder , exist_ok = True )
template_json = f ' { template_default_folder } /template.json '
if os . path . isfile ( template_json ) :
os . unlink ( template_json )
with open ( template_json , ' w ' ) as s :
s . write ( template_json_contents )
default_name_bus_dir = f ' { template_default_folder } /Template/Code/Include/ ' + ' $ {Name} '
os . makedirs ( default_name_bus_dir , exist_ok = True )
default_name_bus_file = f ' { default_name_bus_dir } / ' + ' $ {Name} Bus.h '
if os . path . isfile ( default_name_bus_file ) :
os . unlink ( default_name_bus_file )
with open ( default_name_bus_file , ' w ' ) as s :
s . write ( templated_contents )
restricted_template_default_folder = f ' { dev_root } /restricted/Salem/Templates/DefaultProject '
os . makedirs ( restricted_template_default_folder , exist_ok = True )
restricted_template_json = f ' { restricted_template_default_folder } /template.json '
if os . path . isfile ( restricted_template_json ) :
os . unlink ( restricted_template_json )
with open ( restricted_template_json , ' w ' ) as s :
s . write ( restricted_template_json_contents )
restricted_default_name_bus_dir = f ' { restricted_template_default_folder } /Template/Code/Include/Platform/Salem '
os . makedirs ( restricted_default_name_bus_dir , exist_ok = True )
restricted_default_name_bus_file = f ' { restricted_default_name_bus_dir } / ' + ' $ {Name} Bus.h '
if os . path . isfile ( restricted_default_name_bus_file ) :
os . unlink ( restricted_default_name_bus_file )
with open ( restricted_default_name_bus_file , ' w ' ) as s :
s . write ( templated_contents )
result = engine_template . create_project ( dev_root , ' TestProject ' , keep_license_text = keep_license_text )
if expect_failure :
assert result != 0
else :
assert result == 0
test_project_folder = f ' { dev_root } /TestProject '
assert os . path . isdir ( test_project_folder )
)
def test_create_from_template ( self , tmpdir , concrete_contents , templated_contents , keep_license_text , force ,
expect_failure , template_json_contents ) :
test_project_bus_file = f ' { test_project_folder } /Code/Include/TestProject/TestProjectBus.h '
assert os . path . isfile ( test_project_bus_file )
with open ( test_project_bus_file , ' r ' ) as s :
s_data = s . read ( )
assert s_data == concrete_contents
restricted_test_project_bus_folder = f ' { dev_root } /restricted/Salem/TestProject/Code/Include/Platform/Salem '
assert os . path . isdir ( restricted_test_project_bus_folder )
self . instantiate_template_wrapper ( tmpdir , engine_template . create_from_template , ' TestTemplate ' , concrete_contents ,
templated_contents , keep_license_text , force , expect_failure ,
template_json_contents , destination_name = ' TestTemplate ' )
restricted_default_name_bus_file = f ' { restricted_test_project_bus_folder } /TestProjectBus.h '
assert os . path . isfile ( restricted_default_name_bus_file )
with open ( restricted_default_name_bus_file , ' r ' ) as s :
s_data = s . read ( )
assert s_data == concrete_contents
@pytest.mark.parametrize (
" concrete_contents, templated_contents, "
" keep_license_text, force, expect_failure, "
" template_json_contents " , [
pytest . param ( TEST_CONCRETE_TESTPROJECT_TEMPLATE_CONTENT_WITH_LICENSE , TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
True , True , False ,
TEST_TEMPLATE_JSON_CONTENTS ) ,
pytest . param ( TEST_CONCRETE_TESTPROJECT_TEMPLATE_CONTENT_WITHOUT_LICENSE , TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
False , True , False ,
TEST_TEMPLATE_JSON_CONTENTS )
]
)
def test_create_project ( self , tmpdir , concrete_contents , templated_contents , keep_license_text , force ,
expect_failure , template_json_contents ) :
template_file_map = { ' project.json ' :
'''
{
" project_name " : " $ {Name} "
}
''' }
@pytest.mark.parametrize (
# Append the project.json to the list of files to copy from the template
template_json_dict = json . loads ( template_json_contents )
template_json_dict . setdefault ( ' copyFiles ' , [ ] ) . append (
{
" file " : " project.json " ,
" origin " : " project.json " ,
" isTemplated " : True ,
" isOptional " : False
} )
# Convert the python dictionary back into a json string
template_json_contents = json . dumps ( template_json_dict , indent = 4 )
self . instantiate_template_wrapper ( tmpdir , engine_template . create_project , ' TestProject ' , concrete_contents ,
templated_contents , keep_license_text , force , expect_failure ,
template_json_contents , template_file_map , project_name = ' TestProject ' )
@pytest.mark.parametrize (
" concrete_contents, templated_contents, "
" keep_license_text, expect_failure, "
" template_json_contents, restricted_template_json_contents " , [
" keep_license_text, force, expect_failure, "
" template_json_contents " , [
pytest . param ( TEST_CONCRETE_TESTGEM_TEMPLATE_CONTENT_WITH_LICENSE , TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
True , False ,
TEST_DEFAULTGEM_TEMPLATE_JSON_CONTENTS , TEST_DEFAULTGEM_TEMPLATE_RESTRICTED_JSON_CONTENTS ) ,
True , True , False ,
TEST_TEMPLATE_JSON_CONTENTS ) ,
pytest . param ( TEST_CONCRETE_TESTGEM_TEMPLATE_CONTENT_WITHOUT_LICENSE , TEST_TEMPLATED_CONTENT_WITH_LICENSE ,
False , False ,
TEST_DEFAULTGEM_TEMPLATE_JSON_CONTENTS , TEST_DEFAULTGEM_TEMPLATE_RESTRICTED_JSON_CONTENTS )
Fals e, Tru e, False ,
TEST_TEMPLATE _JSON_CONTENTS)
]
)
def test_create_gem ( tmpdir ,
concrete_contents , templated_contents ,
keep_license_text , expect_failure ,
template_json_contents , restricted_template_json_contents ) :
dev_root = str ( tmpdir . join ( ' dev ' ) . realpath ( ) ) . replace ( ' \\ ' , ' / ' )
os . makedirs ( dev_root , exist_ok = True )
template_default_folder = f ' { dev_root } /Templates/DefaultGem '
os . makedirs ( template_default_folder , exist_ok = True )
template_json = f ' { template_default_folder } /template.json '
if os . path . isfile ( template_json ) :
os . unlink ( template_json )
with open ( template_json , ' w ' ) as s :
s . write ( template_json_contents )
default_name_bus_dir = f ' { template_default_folder } /Template/Code/Include/ ' + ' $ {Name} '
os . makedirs ( default_name_bus_dir , exist_ok = True )
default_name_bus_file = f ' { default_name_bus_dir } / ' + ' $ {Name} Bus.h '
if os . path . isfile ( default_name_bus_file ) :
os . unlink ( default_name_bus_file )
with open ( default_name_bus_file , ' w ' ) as s :
s . write ( templated_contents )
restricted_template_default_folder = f ' { dev_root } /restricted/Salem/Templates/DefaultGem '
os . makedirs ( restricted_template_default_folder , exist_ok = True )
restricted_template_json = f ' { restricted_template_default_folder } /template.json '
if os . path . isfile ( restricted_template_json ) :
os . unlink ( restricted_template_json )
with open ( restricted_template_json , ' w ' ) as s :
s . write ( restricted_template_json_contents )
restricted_default_name_bus_dir = f ' { restricted_template_default_folder } /Template/Code/Include/Platform/Salem '
os . makedirs ( restricted_default_name_bus_dir , exist_ok = True )
restricted_default_name_bus_file = f ' { restricted_default_name_bus_dir } / ' + ' $ {Name} Bus.h '
if os . path . isfile ( restricted_default_name_bus_file ) :
os . unlink ( restricted_default_name_bus_file )
with open ( restricted_default_name_bus_file , ' w ' ) as s :
s . write ( templated_contents )
result = engine_template . create_gem ( dev_root , ' TestGem ' , keep_license_text = keep_license_text )
if expect_failure :
assert result != 0
else :
assert result == 0
test_gem_folder = f ' { dev_root } /Gems/TestGem '
assert os . path . isdir ( test_gem_folder )
test_gem_bus_file = f ' { test_gem_folder } /Code/Include/TestGem/TestGemBus.h '
assert os . path . isfile ( test_gem_bus_file )
with open ( test_gem_bus_file , ' r ' ) as s :
s_data = s . read ( )
assert s_data == concrete_contents
restricted_test_gem_bus_folder = f ' { dev_root } /restricted/Salem/Gems/TestGem/Code/Include/Platform/Salem '
assert os . path . isdir ( restricted_test_gem_bus_folder )
)
def test_create_gem ( self , tmpdir , concrete_contents , templated_contents , keep_license_text , force ,
expect_failure , template_json_contents ) :
# Create a gem.json file in the template folder
template_file_map = { ' gem.json ' :
'''
{
" gem_name " : " $ {Name} "
}
''' }
restricted_default_name_bus_file = f ' { restricted_test_gem_bus_folder } /TestGemBus.h '
assert os . path . isfile ( restricted_default_name_bus_file )
with open ( restricted_default_name_bus_file , ' r ' ) as s :
s_data = s . read ( )
assert s_data == concrete_contents
# Append the gem.json to the list of files to copy from the template
template_json_dict = json . loads ( template_json_contents )
template_json_dict . setdefault ( ' copyFiles ' , [ ] ) . append (
{
" file " : " gem.json " ,
" origin " : " gem.json " ,
" isTemplated " : True ,
" isOptional " : False
} )
self . instantiate_template_wrapper ( tmpdir , engine_template . create_gem , ' TestGem ' , concrete_contents ,
templated_contents , keep_license_text , force , expect_failure ,
template_json_contents , gem_name = ' TestGem ' )