<commit_before>#!/usr/bin/env python
##
## This file is part of ContextKit.
##
## Copyright (C) 2008, 2009 Nokia. All rights reserved.
##
## Contact: <NAME> <<EMAIL>>
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public License
## version 2.1 as published by the Free Software Foundation.
##
## This library is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public
## License along with this library; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
## 02110-1301 USA
##
##
## This test:
##	 - starts up a client and a provider
##	 - starts a commander with a different value and type for a provider
##	   provided property and with a new string non-provided property
##	 - checks that the client gets an error for the provider provided property
##	 - checks that the client gets the new string non-provided property
##	 - changes the non-provided property's type to int and checks it
##	   (so this proves that type checks are always off for commander properties)

import sys
import os
import signal
import re

import unittest
from subprocess import Popen, PIPE

def timeoutHandler(signum, frame):
	raise Exception('tests has been running for too long')

class Callable:
	def __init__(self, anycallable):
		self.__call__ = anycallable

class CommanderNonExistent(unittest.TestCase):
	def startProvider(busname, args):
		ret = Popen(["context-provide", busname] + args,
			  stdin=PIPE,stderr=PIPE,stdout=PIPE)
		# wait for it
		print >>ret.stdin, "info()"
		ret.stdout.readline().rstrip()
		return ret
	startProvider = Callable(startProvider)

	def wanted(name, type, value):
		return "%s = %s:%s" % (name, type, value)
	wanted = Callable(wanted)

	def wantedUnknown(name):
		return "%s is Unknown" % (name)
	wantedUnknown = Callable(wantedUnknown)

	def setUp(self):
		os.environ["CONTEXT_PROVIDE_REGISTRY_FILE"] = "./context-provide.context"
		self.flexiprovider = self.startProvider("com.nokia.test",
										   ["int","test.int","42"])



		os.environ["CONTEXT_PROVIDE_REGISTRY_FILE"] = "./commander.context"
		self.context_commander = self.startProvider("org.freedesktop.ContextKit.Commander",
													["string", "test.int", "foobar",
													 "string", "test.string", "barfoo"])
		self.context_client = Popen(["context-listen","test.int", "test.string"],
									stdin=PIPE, stdout=PIPE, stderr=PIPE)

	def tearDown(self):
		os.kill(self.flexiprovider.pid, 9)
		if (self.context_commander != 0): os.kill(self.context_commander.pid, 9)
		os.kill(self.context_client.pid, 9)
		os.unlink('context-provide.context')
		#os.unlink('../commander.context')

	def testCommanderFunctionality(self):
		line = self.context_client.stderr.readline().rstrip()
		while not 'Provider error, bad type for  test.int wanted: INT got: QString' in line:
			line = self.context_client.stderr.readline().rstrip()

		# if we are here, then the type check worked

		# check the non-existent property
		got = self.context_client.stdout.readline().rstrip()
		self.assertEqual(got,
						 self.wanted("test.string", "QString", "barfoo"),
						 "Non-existent property couldn't be commanded")

		# change the type of the non-existent property
		print >>self.context_commander.stdin, "add(INT('test.string', 42))"
		got = self.context_client.stdout.readline().rstrip()
		self.assertEqual(got,
						  self.wanted("test.string", "int", "42"),
						  "Non-existent property's type couldn't be overwritten")
<commit_msg>libcontextsubscriber/customer-tests: fix for the new logging framework.<commit_after>#!/usr/bin/env python
##
## This file is part of ContextKit.
##
## Copyright (C) 2008, 2009 Nokia. All rights reserved.
##
## Contact: <NAME> <<EMAIL>>
##
## This library is free software; you can redistribute it and/or
## modify it under the terms of the GNU Lesser General Public License
## version 2.1 as published by the Free Software Foundation.
##
## This library is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## Lesser General Public License for more details.
##
## You should have received a copy of the GNU Lesser General Public
## License along with this library; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
## 02110-1301 USA
##
##
## This test:
##	 - starts up a client and a provider
##	 - starts a commander with a different value and type for a provider
##	   provided property and with a new string non-provided property
##	 - checks that the client gets an error for the provider provided property
##	 - checks that the client gets the new string non-provided property
##	 - changes the non-provided property's type to int and checks it
##	   (so this proves that type checks are always off for commander properties)

import sys
import os
import signal
import re

import unittest
from subprocess import Popen, PIPE

def timeoutHandler(signum, frame):
	raise Exception('tests has been running for too long')

class Callable:
	def __init__(self, anycallable):
		self.__call__ = anycallable

class CommanderNonExistent(unittest.TestCase):
	def startProvider(busname, args):
		ret = Popen(["context-provide", busname] + args,
			  stdin=PIPE,stderr=PIPE,stdout=PIPE)
		# wait for it
		print >>ret.stdin, "info()"
		ret.stdout.readline().rstrip()
		return ret
	startProvider = Callable(startProvider)

	def wanted(name, type, value):
		return "%s = %s:%s" % (name, type, value)
	wanted = Callable(wanted)

	def wantedUnknown(name):
		return "%s is Unknown" % (name)
	wantedUnknown = Callable(wantedUnknown)

	def setUp(self):
		os.environ["CONTEXT_PROVIDE_REGISTRY_FILE"] = "./context-provide.context"
		self.flexiprovider = self.startProvider("com.nokia.test",
										   ["int","test.int","42"])



		os.environ["CONTEXT_PROVIDE_REGISTRY_FILE"] = "./commander.context"
		self.context_commander = self.startProvider("org.freedesktop.ContextKit.Commander",
													["string", "test.int", "foobar",
													 "string", "test.string", "barfoo"])
		self.context_client = Popen(["context-listen","test.int", "test.string"],
									stdin=PIPE, stdout=PIPE, stderr=PIPE)

	def tearDown(self):
		os.kill(self.flexiprovider.pid, 9)
		if (self.context_commander != 0): os.kill(self.context_commander.pid, 9)
		os.kill(self.context_client.pid, 9)
		os.unlink('context-provide.context')
		#os.unlink('../commander.context')

	def testCommanderFunctionality(self):
		line = self.context_client.stderr.readline().rstrip()
		while not 'Provider error, bad type for  "test.int" wanted: "INT" got: QString' in line:
			line = self.context_client.stderr.readline().rstrip()

		# if we are here, then the type check worked

		# check the non-existent property
		got = self.context_client.stdout.readline().rstrip()
		self.assertEqual(got,
						 self.wanted("test.string", "QString", "barfoo"),
						 "Non-existent property couldn't be commanded")

		# change the type of the non-existent property
		print >>self.context_commander.stdin, "add(INT('test.string', 42))"
		got = self.context_client.stdout.readline().rstrip()
		self.assertEqual(got,
						  self.wanted("test.string", "int", "42"),
						  "Non-existent property's type couldn't be overwritten")

{'fraction_non_alphanumeric': 0.12093389296956977, 'fraction_numerical': 0.00813221406086044, 'mean_word_length': 4.280470914127424, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3110712', 'n_tokens_mistral': 2579, 'n_tokens_neox': 2260, 'n_words': 758}