Skip to content

Commit

Permalink
test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
David Glick committed Dec 4, 2019
1 parent 1536804 commit e98e2b8
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 184 deletions.
11 changes: 8 additions & 3 deletions cumulusci/tasks/salesforce/BaseRetrieveMetadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from cumulusci.core.utils import process_bool_arg
from cumulusci.tasks.salesforce import BaseSalesforceMetadataApiTask
from cumulusci.utils import inject_namespace
from cumulusci.utils import zip_strip_namespace
from cumulusci.utils import strip_namespace
from cumulusci.utils import process_text_in_zipfile
from cumulusci.utils import tokenize_namespace

Expand Down Expand Up @@ -63,8 +63,13 @@ def _process_namespace(self, src_zip):
),
)
if self.options.get("namespace_strip"):
src_zip = zip_strip_namespace(
src_zip, self.options["namespace_strip"], logger=self.logger
src_zip = process_text_in_zipfile(
src_zip,
functools.partial(
strip_namespace,
namespace=self.options["namespace_strip"],
logger=self.logger,
),
)
return src_zip

Expand Down
11 changes: 8 additions & 3 deletions cumulusci/tasks/salesforce/Deploy.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from cumulusci.utils import temporary_dir
from cumulusci.utils import zip_clean_metaxml
from cumulusci.utils import inject_namespace
from cumulusci.utils import zip_strip_namespace
from cumulusci.utils import strip_namespace
from cumulusci.utils import tokenize_namespace
from cumulusci.utils import process_text_in_zipfile

Expand Down Expand Up @@ -144,8 +144,13 @@ def _process_namespace(self, zipf):
),
)
if self.options.get("namespace_strip"):
zipf = zip_strip_namespace(
zipf, self.options["namespace_strip"], logger=self.logger
zipf = process_text_in_zipfile(
zipf,
functools.partial(
strip_namespace,
namespace=self.options["namespace_strip"],
logger=self.logger,
),
)
return zipf

Expand Down
11 changes: 7 additions & 4 deletions cumulusci/tasks/salesforce/UpdateDependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from cumulusci.utils import download_extract_zip
from cumulusci.utils import download_extract_github
from cumulusci.utils import inject_namespace
from cumulusci.utils import zip_strip_namespace
from cumulusci.utils import strip_namespace
from cumulusci.utils import process_text_in_zipfile
from cumulusci.utils import tokenize_namespace

Expand Down Expand Up @@ -286,10 +286,13 @@ def _install_dependency(self, dependency):
"{}__".format(dependency["namespace_strip"])
)
)
package_zip = zip_strip_namespace(
package_zip = process_text_in_zipfile(
package_zip,
namespace=dependency["namespace_strip"],
logger=self.logger,
functools.partial(
strip_namespace,
namespace=dependency["namespace_strip"],
logger=self.logger,
),
)

package_zip = ZipfilePackageZipBuilder(package_zip)()
Expand Down
2 changes: 1 addition & 1 deletion cumulusci/tasks/salesforce/sourcetracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def _run_task(self):
)

# Reinject namespace tokens
if self.options("namespace_tokenize"):
if self.options.get("namespace_tokenize"):
process_text_in_directory(
target,
functools.partial(
Expand Down
130 changes: 52 additions & 78 deletions cumulusci/tasks/salesforce/tests/test_sourcetracking.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import io
from unittest import mock
import json
import os
import unittest
import zipfile

from cumulusci.core.config import OrgConfig
from cumulusci.tasks.salesforce.sourcetracking import ListChanges
from cumulusci.tasks.salesforce.sourcetracking import RetrieveChanges
from cumulusci.tasks.salesforce.sourcetracking import SnapshotChanges
from cumulusci.tasks.salesforce.tests.util import create_task
from cumulusci.tests.util import create_project_config
from cumulusci.utils import temporary_dir


Expand Down Expand Up @@ -87,7 +87,7 @@ def test_run_task__snapshot(self):
],
}
task._run_task()
self.assertIn("Ignored 1 changed components in the scratch org.", messages)
self.assertIn("Found no changes.", messages)

def test_filter_changes__include(self):
foo = {"MemberType": "CustomObject", "MemberName": "foo__c", "RevisionNum": 1}
Expand All @@ -98,7 +98,7 @@ def test_filter_changes__include(self):
"RevisionNum": 1,
}
task = create_task(ListChanges, {"include": "foo", "exclude": "bar"})
filtered = task._filter_changes({"records": [foo, bar, foobar]})
filtered, ignored = task._filter_changes([foo, bar, foobar])
self.assertEqual([foo], filtered)

def test_filter_changes__null_revnum(self):
Expand All @@ -109,25 +109,40 @@ def test_filter_changes__null_revnum(self):
}
bar = {"MemberType": "CustomObject", "MemberName": "bar__c", "RevisionNum": 1}
task = create_task(ListChanges, {})
filtered = task._filter_changes({"records": [foo, bar]})
filtered, ignored = task._filter_changes([foo, bar])
self.assertEqual([foo, bar], filtered)

self.assertEqual(-1, task._snapshot["CustomObject"]["foo__c"])
filtered = task._filter_changes({"records": [foo, bar]})
self.assertEqual([], filtered)

foo["RevisionNum"] = 12
filtered = task._filter_changes({"records": [foo, bar]})
self.assertEqual([foo], filtered)
def test_load_maxrevision(self):
with temporary_dir():
task = create_task(ListChanges, {})
task._store_maxrevision(1)
assert task._load_maxrevision() == 1


@mock.patch("cumulusci.tasks.salesforce.sourcetracking.sfdx")
class TestRetrieveChanges(unittest.TestCase):
"""Retrieve changed components from a scratch org"""

def test_run_task(self):
with temporary_dir() as path:
os.mkdir("src")
task = create_task(RetrieveChanges, {"path": "src", "include": "Test"})
def test_init_options__sfdx_format(self, sfdx):
with temporary_dir():
project_config = create_project_config()
project_config.project__source_format = "sfdx"
with open("sfdx-project.json", "w") as f:
json.dump(
{"packageDirectories": [{"path": "force-app", "default": True}]}, f
)
task = create_task(RetrieveChanges, {}, project_config)
assert not task.md_format
assert task.options["path"] == "force-app"

def test_run_task(self, sfdx):
sfdx_calls = []
sfdx.side_effect = lambda cmd, *args, **kw: sfdx_calls.append(cmd)

with temporary_dir():
task = create_task(
RetrieveChanges, {"include": "Test", "namespace_tokenize": "ns"}
)
task._init_task()
task.tooling = mock.Mock()
task.tooling.query_all.return_value = {
Expand All @@ -140,61 +155,17 @@ def test_run_task(self):
}
],
}
zf = zipfile.ZipFile(io.BytesIO(), "w")
zf.writestr("objects/Test__c.object", "<root />")
task.api_class = mock.Mock(return_value=mock.Mock(return_value=zf))
task._run_task()
with open(os.path.join(path, "src", "package.xml"), "r") as f:
package_xml = f.read()
self.assertIn("<members>Test__c</members>", package_xml)

def test_run_task__merge_changes(self):
# If there is already an existing package,
# we should add to it rather than overwriting it.
with temporary_dir() as path:
with open("package.xml", "w") as f:
f.write(
"""<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Object1</members>
<name>CustomObject</name>
</types>
</Package>
"""
)
task = create_task(RetrieveChanges, {"path": path})
task._init_task()
task.tooling = mock.Mock()
task.tooling.query_all.return_value = {
"totalSize": 1,
"records": [
{
"MemberType": "CustomObject",
"MemberName": "Object2",
"RevisionNum": 1,
}
],
}
task.api_class = mock.Mock()
task._run_task()

package_xml = task.api_class.call_args[0][1]
self.maxDiff = None
self.assertEqual(
"""<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Object1</members>
<members>Object2</members>
<name>CustomObject</name>
</types>
<version>47.0</version>
</Package>""",
package_xml,
)
assert sfdx_calls == [
"force:mdapi:convert",
"force:source:retrieve",
"force:source:convert",
]
assert os.path.exists(os.path.join("src", "package.xml"))

def test_run_task__no_changes(self):
def test_run_task__no_changes(self, sfdx):
with temporary_dir() as path:
task = create_task(RetrieveChanges, {"path": path})
task._init_task()
Expand Down Expand Up @@ -222,17 +193,20 @@ def test_run_task(self):
task = create_task(SnapshotChanges, org_config=org_config)
task._init_task()
task.tooling.query = mock.Mock(
return_value={
"totalSize": 1,
"done": True,
"records": [
{
"MemberType": "CustomObject",
"MemberName": "Object2",
"RevisionNum": 1,
}
],
}
side_effect=[
{"totalSize": 0, "records": [], "done": True},
{
"totalSize": 1,
"done": True,
"records": [
{
"MemberType": "CustomObject",
"MemberName": "Object2",
"RevisionNum": 1,
}
],
},
]
)
task._run_task()
self.assertTrue(
Expand Down
Loading

0 comments on commit e98e2b8

Please sign in to comment.