Skip to content

Instantly share code, notes, and snippets.

View hlindberg's full-sized avatar

Henrik Lindberg hlindberg

  • Bebalon Ltd
  • Għarb, Gozo, Malta
  • X @hel
View GitHub Profile
@hlindberg
hlindberg / sample_mocking_spec.rb
Last active October 4, 2021 07:17
An rspec sample showing some override and mocking of 4.x functions/function loading
require 'spec_helper'
# Example rspec_puppet function rspec test (i.e. subject is the function 'min')
# This works for other rspec subjects as well as a compiler is always involved.
# This kind of mocking can be required when it is not enough to simply override
# a function with another implementation (which can be done with a `let(:pre_condition) { 'function min($x, $y) { ... }'}`
#
# The main difficulty that this overcomes is the need to let the compiler initialize and
# create the context in which it will operate before making any mocks.
#
@hlindberg
hlindberg / comma_separated_list.java
Last active September 17, 2018 18:39
Java Snippets
StringJoiner joiner = new StringJoiner(",");
for (Item item : list) {
joiner.add(item.toString());
}
return joiner.toString();
@hlindberg
hlindberg / output.md
Last active September 2, 2018 17:02
Stringified output
expression type example output
literal undef nil
literal default "default"
literal integer 1
literal float 3.14
literal false false
literal true true
literal string "hello"
literal regexp "/this|that.*/"
@hlindberg
hlindberg / set_github_labels.js
Last active June 16, 2019 08:27
Script to run in browser dev-mode when on label edit page for a github repo
[
{
'name': 'bug',
'color': 'd63230',
'desc': 'Does not work (according to specification)'
},
{
'name': 'improvement',
'color': '39a9db',
'desc': 'improvement - neither bug nor new feature'
@hlindberg
hlindberg / myjoin.pp
Last active September 4, 2017 15:19
function myjoin(Array $a, String $delimiter) {
$formats = {
Array => {
# no delimiters = '% ', and 'a' means array
format => '% a',
separator => $delimiter,
# format strings without delimiters = '%s'
string_formats => { String => '%s' }
}
function mymodule::my_privates (
Variant[String, Numeric] $key,
Hash $options,
Puppet::LookupContext $context,
) {
case $key {
'mymodule::secret1' : { Sensitive('There is no Santa on the evening coach') }
'mymodule::secret2' : { Sensitive('All your base are belong to us') }
default: { $context.not_found }
}
<%-
$pin = "release a=${name}" # default value
if $pin_release.length > 0
$options = [
if $release.length > 0 { "a=${release}" },
if $codename.length > 0 { "n=${codename}" },
if $release_version.length > 0 { "v=${release_version}"},
if $component.length > 0 { "c=${component}" },
if $originator.length > 0 { "o=${originator}" },
if $label.length > 0 { "l=${label}" },
Puppet::Parser::Functions.newfunction(:default_content,
:type => :rvalue,
:doc => <<-'ENDOFDOC'
Takes an optional content and an optional template name and returns the
contents of a file.
Examples:
$config_file_content = default_content($file_content, $template_location)
file { '/tmp/x':
@hlindberg
hlindberg / exampleepp.pp
Created December 2, 2016 18:06
An example of using a local variable in EPP
# Example of local variables set inside of an EPP template
#
$template = @(EOF)
<%
# this is an element with arbitrary puppet code
$hash = { a => 1, b => 2, c => 3 }
-%>
The keys are:
<%- $hash.each |$key, $value| { -%>
@hlindberg
hlindberg / my_first_puppet_validator.rb
Created November 29, 2016 23:28
This is an example puppet AST validator for Puppet 4.x showing various use of the Puppet AST and Parser APIs
require 'puppet'
# Example of a module setting everything up to perform custom
# validation of an AST model produced by parsing puppet source.
#
module MyValidation
module Issues
# (see Puppet::Pops::Issues#issue)
# This is boiler plate code
def self.issue (issue_code, *args, &block)