# Buggy code
def rename(name, source, force=False, makedirs=False):
    """
    If the source file exists on the system, rename it to the named file. The
    named file will not be overwritten if it already exists unless the force
    option is set to True.

    name
        The location of the file to rename to

    source
        The location of the file to move to the location specified with name

    force
        If the target location is present then the file will not be moved,
        specify "force: True" to overwrite the target file

    makedirs
        If the target subdirectories don't exist create them

    """
    ret = {"name": name, "changes": {}, "comment": "", "result": True}

    if not os.path.isabs(name):
        return _error(ret, "Specified file {0} is not an absolute path".format(name))

    if not os.path.lexists(source):
        ret["comment"] = (
            'Source file "{0}" has already been moved out of place'
        ).format(source)
        return ret

    if os.path.lexists(source) and os.path.lexists(name):
        if not force:
            ret["comment"] = (
                'The target file "{0}" exists and will not be overwritten'.format(name)
            )
            ret["result"] = False
            return ret
        elif not __opts__["test"]:
            # Remove the destination to prevent problems later
            try:
                if os.path.islink(name):
                    os.unlink(name)
                elif os.path.isfile(name):
                    os.remove(name)
                else:
                    shutil.rmtree(name)
            except (IOError, OSError):
                return _error(
                    ret,
                    'Failed to delete "{0}" in preparation for forced move'.format(
                        name
                    ),
                )

    if __opts__["test"]:
        ret["comment"] = 'File "{0}" is set to be moved to "{1}"'.format(source, name)
        ret["result"] = None
        return ret

    # Run makedirs
    dname = os.path.dirname(name)
    if not os.path.isdir(dname):
        if makedirs:
            __salt__["file.makedirs"](name)
        else:
            return _error(ret, "The target directory {0} is not present".format(dname))
    # All tests pass, move the file into place
    try:
        if os.path.islink(source):
            linkto = os.readlink(source)
            os.symlink(linkto, name)
            os.unlink(source)
        else:
            shutil.move(source, name)
    except (IOError, OSError):
        return _error(ret, 'Failed to move "{0}" to "{1}"'.format(source, name))

    ret["comment"] = 'Moved "{0}" to "{1}"'.format(source, name)
    ret["changes"] = {name: source}
    return ret


# Traceback (TypeError)
2014-11-24 16:03:42,245 [salt.minion      ][WARNING ] TypeError encountered executing state.highstate: expected string or buffer. See debug log for more info.  Possibly a missing arguments issue:  ArgSpec(args=['test', 'queue'], varargs=None, keywords='kwargs', defaults=(None, False))
Traceback (most recent call last):
File "/usr/lib/pymodules/python2.7/salt/minion.py", line 809, in _thread_return
return_data = func(*args, **kwargs)
File "/usr/lib/pymodules/python2.7/salt/modules/state.py", line 275, in highstate
force=kwargs.get('force', False)
File "/usr/lib/pymodules/python2.7/salt/state.py", line 2546, in call_highstate
return self.state.call_high(high)
File "/usr/lib/pymodules/python2.7/salt/state.py", line 1715, in call_high
ret = self.call_chunks(chunks)
File "/usr/lib/pymodules/python2.7/salt/state.py", line 1437, in call_chunks
running = self.call_chunk(low, running, chunks)
File "/usr/lib/pymodules/python2.7/salt/state.py", line 1562, in call_chunk
status = self.check_requisite(low, running, chunks, True)
File "/usr/lib/pymodules/python2.7/salt/state.py", line 1489, in check_requisite
if (fnmatch.fnmatch(chunk['name'], req_val) or
File "/usr/lib/python2.7/fnmatch.py", line 43, in fnmatch
return fnmatchcase(name, pat)
File "/usr/lib/python2.7/fnmatch.py", line 79, in fnmatchcase
return _cache[pat].match(name) is not None
TypeError: expected string or buffer

# Fixed code
def rename(name, source, force=False, makedirs=False):
    """
    If the source file exists on the system, rename it to the named file. The
    named file will not be overwritten if it already exists unless the force
    option is set to True.

    name
        The location of the file to rename to

    source
        The location of the file to move to the location specified with name

    force
        If the target location is present then the file will not be moved,
        specify "force: True" to overwrite the target file

    makedirs
        If the target subdirectories don't exist create them

    """
    ret = {"name": name, "changes": {}, "comment": "", "result": True}
    if not name:
        return _error(ret, "Must provide name to file.rename")

    if not os.path.isabs(name):
        return _error(ret, "Specified file {0} is not an absolute path".format(name))

    if not os.path.lexists(source):
        ret["comment"] = (
            'Source file "{0}" has already been moved out of place'
        ).format(source)
        return ret

    if os.path.lexists(source) and os.path.lexists(name):
        if not force:
            ret["comment"] = (
                'The target file "{0}" exists and will not be overwritten'.format(name)
            )
            ret["result"] = False
            return ret
        elif not __opts__["test"]:
            # Remove the destination to prevent problems later
            try:
                if os.path.islink(name):
                    os.unlink(name)
                elif os.path.isfile(name):
                    os.remove(name)
                else:
                    shutil.rmtree(name)
            except (IOError, OSError):
                return _error(
                    ret,
                    'Failed to delete "{0}" in preparation for forced move'.format(
                        name
                    ),
                )

    if __opts__["test"]:
        ret["comment"] = 'File "{0}" is set to be moved to "{1}"'.format(source, name)
        ret["result"] = None
        return ret

    # Run makedirs
    dname = os.path.dirname(name)
    if not os.path.isdir(dname):
        if makedirs:
            __salt__["file.makedirs"](name)
        else:
            return _error(ret, "The target directory {0} is not present".format(dname))
    # All tests pass, move the file into place
    try:
        if os.path.islink(source):
            linkto = os.readlink(source)
            os.symlink(linkto, name)
            os.unlink(source)
        else:
            shutil.move(source, name)
    except (IOError, OSError):
        return _error(ret, 'Failed to move "{0}" to "{1}"'.format(source, name))

    ret["comment"] = 'Moved "{0}" to "{1}"'.format(source, name)
    ret["changes"] = {name: source}
    return ret