How to compute a value from the src::branch property when definition a build step - buildbot

I must compute a value for a build step based on the src::branch property and based on the available documentation this only seems to be possible by defining custom renderables.
I created a custom renderable defined as follows:
#implementer(IRenderable)
class DetermineVersion(object):
def getRenderingFor(self, props):
if props.hasProperty("src::branch"):
return "--version=" + props['src::branch'].lower().replace("tag/", "")
else:
raise Exception("The property 'branch' (tag/version) must be set")
and used it as follows in a build step:
f.addStep(steps.ShellCommand(
name="create_tag",
command=["python", "createTag.py", DetermineVersion()],
))
Unfortunately this does not seem to work as expected and regardless of the fact of the "branch" property is set or not, I always see the exception thrown by my getRenderingFor function.

I used wrong property name src::branch instead of branch:
This works as expected:
#implementer(IRenderable)
class DetermineVersion(object):
def getRenderingFor(self, props):
if props.hasProperty("branch"):
return "--version=" + props['branch'].lower().replace("tag/", "")
else:
raise Exception("The property 'branch' (tag/version) must be set")

Related

Finding partial attribute value in Katalon

Trying to find a partial attribute value. Full value is no problem.
I have h1 class="a b c" and want to find out, whether this h1 has a as a class attribute.
Trying WebUI.verifyMatch(findTestObject('mytest/mytest-h1'),'a', 'a.*', false, FailureHandling.STOP_ON_FAILURE) and fails on finding.
Also answered via this Katalon Forum post (Apologies if the link is broken, in the future).
As per Mate Mrše's answer you can also try the following:
def attribute = WebUI.getAttribute(findTestObject('mytest/mytest-h1'), 'class')
boolean doesAttributeExist = attribute.contains('a')
if (!doesAttributeExist) {
// Add some logic/interaction
}
Since you added FailureHandling.STOP_ON_FAILURE the test will fail regardless of the condition.
Should you want the test to continue rather use FailureHandling.OPTIONAL
Try this:
def attribute = WebUI.getAttribute(findTestObject('mytest/mytest-h1'), 'class')
assert attribute.contains('a ')
Alternatively, create an object using the CSS class as the locator and verify the element exists:
assert WebUI.verifyElementClickable(findTestObject('mytest/mytest-h1-a')) == true

Supporting "recursive objects" in lua

I'm fairly new to lua and have the following problem with an assignment from a class:
We currently extend lua to support objects and inheritance. The Syntax for that is
Class{'MyClass',
attribute1 = String,
attribute2 = Number
}
Class{'MySubClass', MyClass,
attribute3 = Number
}
This works perfectly fine. The real problem lies within the next task: We should support "recursive types", that means a call like
Class{'MyClass', attribute = MyClass}
should result in an class with a field of the same type as the class. When this "class-constructor" is called the variable MyClass is nil, thats why the parameter table doesnt't have an entry attribute. How is it possible to access this attribute?
My first thought was using some kind of nil-table which gets returned every time the global __index is called with an unset key. This nil-table should behave like the normal nil, but can be checked for in the "class-constructor". The problem with this approach are comparisons like nil == unknown. This should return true, but as the __eq meta method of the nil-table is never called we cannot return true.
Is there another approach I'm currently just ignoring? Any hint is greatly appreciated.
Thanks in advance.
Edit:
Here the relevant part of the "testfile". The test by which the code is rated in class is another one and gets published later.
three = 3
print( three == 3 , "Should be true")
print( unknown == nil , "Should be true" )
Class{'AClass', name = String, ref = AClass}
function AClass:write()
print("AClass:write(), name of AClass:", self.name)
end
aclass = AClass:create("A. Class")
aclass:write()
Since MyClass is just a lookup in the global table (_G), you could mess with its metatable's __index to return a newly-defined MyClass object (which you would later need to fill with the details).
However, while feasible, such an implementation is
wildly unsafe, as you could end up with an undefined class (or worse, you may end up inadvertantly creating an infinite lookup loop. Trust me, I've been there)
very hard to debug, as every _G lookup for a non-existing variable will now return a newly created class object instead of nil (this problem could somewhat be reduced by requiring that class names start with an uppercase character)
If you go that route, be sure to also override __newindex.
How about providing the argument in string form?
Class{'MyClass', attribute = 'MyClass'}
Detect strings inside the implementation of Class and process them with _G[string] after creating the class
Or alternatively, use a function to delay the lookup:
Class{'MyClass', attribute = function() return MyClass end}

How do I cast to an interface an object may implement?

I have the following classes & interfaces:
export interface IBody {
body : ListBody;
}
export class Element {
// ...
}
export class Paragraph extends Element implements IBody {
// ...
}
export class Character extends Element {
// ...
}
I have code where I will get an array of Element derived objects (there are more than just Paragraph & Character). In the case of those that implement IBody, I need to take action on the elements in the body.
What is the best way to see if it implements IBody? Is it "if (element.body !== undefined)"?
And then how do I access it? "var bodyElement = <IBody> element;" gives me an error.
C:/src/jenova/Dev/Merge/AutoTagWeb/client/layout/document/elements/factory.ts(34,27): error TS2012: Cannot convert 'Element' to 'IBody':
Type 'Element' is missing property 'body' from type 'IBody'.
Type 'IBody' is missing property 'type' from type 'Element'.
thanks - dave
An interface in TypeScript is a compile-time only construct, with no run-time representation. You might find section 7 of the TypeScript specification interesting to read as it has the complete details.
So, you can't "test" for an interface specifically. Done correctly and completely, you generally shouldn't need to test for it as the compiler should have caught the cases where an object didn't implement the necessary interface. If you were to try using a type assertion:
// // where e has been typed as any, not an Element
var body = <IBody> e;
The compiler will allow it without warning as you've asserted that the type is an IBody. If however, e were an Element in scope, the compiler as you've shown will check the signature of the Element and confirm that it has the properties/methods declared by IBody. It's important to note that it's checking the signature -- it doesn't matter that it may not implement IBody as long as the signature matches up.
Assuming that Element has a signature that matches IBody, it will work. If it does not, you'll get the compiler error you're receiving. But, again, if it's declared as any, the assertion will pass and at run-time, unless the type has the methods defined on IBody, the script will fail.
As your Element is the base class, you cannot check for IBody. You could declare an argument as any:
function someFeature(e: any) {
}
And then assert that the IBody is present:
function someFeature(e: any) {
var body :IBody = <IBody> e;
// do something
}
However, if you do need a run-time check, you'd need to look for the function on the prototype or as a property before using it. While that could be misleading in some cases, the interface in TypeScript also may not have caught the mismatch either. Here's an example of how you could check for the existence of a specific function.
It might look like this:
function someFeature(e: any) {
var body = <IBody> e;
if (typeof (body.someFunctionOnBodyInterface) === "undefined") {
// not safe to use the function
throw new Error("Yikes!");
}
body.someFunctionOnBodyInterface();
}

FactoryGirl to_create return value

From my understanding, the return value from a factory's 'to_create' method is ignored. This means that the object returned from the 'build' or 'initialize_with' portion of the factory is the object ultimately returned when calling 'create' within a test.
In my case, I am using a variant of the Repository Pattern. I am overriding the 'to_create' portion of the factory to include a call to a repository 'save' method. This method does not modify the given object, but returns an object representing the persisted form of the original.
However, the instance returned from the 'build' block is returned from the factory, and not the instance created in the 'to_create' block. In my code, this means the "unpersisted" form of the object is returned, not the object with updated attributes (e.g. 'id') from the saving action.
Is there a way of forcing the return value of 'create' to be either the result of the 'to_create' block or some value generated within that block?
class Foo
attr_accessor :id, :name
...
end
class FooRepository
def self.create(name)
Foo.new(name) # this object is not yet persisted and has no .id
end
def self.save(foo)
# this method must not guarantee that the original Foo instance
# will always be returned
...
updated_foo # this is a duplicate of the original object
end
...
end
FactoryGirl.define do
factory :foo, class: FooRepository do
# create an example Foo
initialize_with { FooRepository.create(name: "Example") }
# save the Foo to the datastore, returning what may be a duplicate
to_create {|instance| FooRepository.save(instance)}
end
end
describe FooRepository do
it "saves the given Foo to the datastore" do
foo = create(:foo)
foo.id #=> nil
...
end
end
I don't have an answer for you beyond "raise an issue", sorry.
The default to_create callback looks like this:
$ grep to_create lib/factory_girl/configuration.rb
to_create {|instance| instance.save! }
The main problem is that ActiveRecord modifies itself in place when you call save! on it. FactoryGirl will ignore any new objects that are returned from to_create.
A quick hack if you want to override the default create strategy:
module FactoryGirl
module Strategy
class Create
def association(runner)
runner.run
end
def result(evaluation)
evaluation.object.tap do |instance|
evaluation.notify(:after_build, instance)
evaluation.notify(:before_create, instance)
instance = evaluation.create(instance) # <-- HACK
evaluation.notify(:after_create, instance)
end
end
end
end
end
... Or do this to your to_create hook to mimic Rails' in-place modification:
to_create do |record|
new_record = YourRepositoryHere.new.create(record)
record.attributes = new_record.attributes # For example
new_record # Return the record just in case the bug is fixed
end
Best of luck. :(

Delete an element in a set by name

I have the following set:
class Element (var Name:String, var Description: String)
var MoreElement: Set[Element] = Set(E1, E2, E3, ...)
How do I delete an Element in a set MoreElement by name.
I found this solution:
MoreElement -= (MoreElement find (_.Name == "nameOfElementToRemove")).get
but I would not use the get, because if you does not find the item is thrown an exception, however I do not want no exception.
MoreElement = MoreElement filterNot (_.Name == "nameOfElementToRemove")
The direct answer to you question is to use filter, meaning something like:
moreElements = moreElements.filter( _.name != "nameOfElementToRemove")
Note this will scan the set. If you want a set indexed by name, you should really use a Map.
However, some caveats:
A set is a collection of unique elements. In order to compare elements in the set, it uses the contained type's equality operator. In your case, the Element class needs to define the 'equals' method (and hashCode) so the set can effectively compare instances.
In addition, you need to keep in mind that Set is an immutable class in Scala, so in your example you're really creating a new set, despite using an operator that appears to modify the existing set.
If you want a mutable set, you need to import scala.collection.mutable.Set.